Question

Ok I am looking for an sql query to insert meta_key & value in a specific table IF another value exist. I am using wordpress database.

I want a query to search the "wp_postmeta" table for "qty (meta_key)" with "0 (meta_value)" and insert to all those posts a new meta_key "_wplp_post_front" with meta_value "1"

I am new with phpmyadmin and I really need help.

________________________UPDATE!!!
Ok once again i have found the solution from third party website:

INSERT INTO wp_postmeta (meta_key, meta_value, post_id)
SELECT "_wplp_post_front", 1, post_id
 FROM wp_postmeta outside_table
 WHERE meta_key = 'qty'
 AND meta_value = '0'
 AND NOT EXISTS 
(SELECT * FROM wp_postmeta
WHERE post_id = outside_table.post_id
AND meta_key = "_wplp_post_front"
AND meta_value = 1
)
Was it helpful?

Solution

Ok once again i have found the solution from third party website:

INSERT INTO wp_postmeta (meta_key, meta_value, post_id)
SELECT "_wplp_post_front", 1, post_id
 FROM wp_postmeta outside_table
 WHERE meta_key = 'qty'
 AND meta_value = '0'
 AND NOT EXISTS 
(SELECT * FROM wp_postmeta
WHERE post_id = outside_table.post_id
AND meta_key = "_wplp_post_front"
AND meta_value = 1
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top