I am trying to bulk insert data into wp_postmeta from the results of select from wp_posts:
- within one INSERT query (I do not want to process data in PHP, only send a request to MySQL)
- not duplicating rows if they exist.
The primary key in wp_postmeta can't be used to check for duplicate keys (it's an autoincrement key and I don't have it in the select data)
So my code is
INSERT INTO `wp_postmeta`
(`post_id`,
`meta_key`,
`meta_value`
)
SELECT
`ID`,
'my_id',
`post_excerpt`,
FROM `wp_posts` WHERE post_status = 'myprefix_pending'
this code works, but duplicates rows on every execution
I have modified it the following way:
INSERT INTO `wp_postmeta`
(`post_id`,
`meta_key`,
`meta_value`
)
SELECT
`ID`,
'my_id',
`post_excerpt`,
FROM `wp_posts` WHERE post_status = 'myprefix_pending' AND NOT EXISTS(SELECT meta_value FROM `wp_postmeta` WHERE ID=`ID` AND meta_key='1C_id')
But this part doesn't work:
AND NOT EXISTS(SELECT meta_value FROM `wp_postmeta` WHERE ID=`ID` AND meta_key='1C_id')
How do I do it the right way?
I've read SQL Multiple WHERE IN but don't understant if it answers my question
Aucun commentaire:
Enregistrer un commentaire