Insert records into a table based on the result of a query


I'm trying to insert a new row with some data for each value of a SELECT.

I leave the last query that has occurred to me but that obviously does not work:

SET @postid = 
(SELECT post_id 
FROM wp_postmeta 
WHERE meta_value
LIKE '%barcelona%');

INSERT INTO wp_postmeta ( post_id, meta_key, meta_value)
                       ( @postid, "geolocation_city", "Barcelona");

That is, what I want is for each result of the select to create a new row in the table wp_postmeta with the value meta_value = Barcelona and meta_key = geolocation_city and the post id should be that of each result of the select.

I hope to be explaining myself well.

Does anyone think of how to do it? Thank you very much.

asked by dev 14.09.2016 в 02:22

1 answer


One option is to use INSERT INTO SELECT .

INSERT INTO wp_postmeta ( post_id, meta_key, meta_value)
SELECT A.post_id, "geolocation_city", "Barcelona"
FROM wp_postmeta A
WHERE A.meta_value LIKE '%barcelona%;'
answered by 14.09.2016 в 02:46