I need to add a new row for all post_ids that equal specific condition. So there is a table with post_id
, meta_key
and meta_value
id | post_id | meta_key | meta_value |
---|---|---|---|
1 | 100 | page-type | city |
2 | 100 | banner | "banner" |
3 | 101 | page-type | city |
4 | 102 | page-type | city |
5 | 102 | banner | "new banner |
6 | 111 | page-type | non-city |
7 | 111 | banner | "non-citybanner" |
I need to add for post_id that has meta_key = "page-type"
and meta_value = "city"
while meta_key = "banner"
does not exist. In this example I should have a new row
id | post_id | meta_key | meta_value |
---|---|---|---|
8 | 101 | banner | "my new banner" |
Of course the table contains about 200K such rows 🙂
Any suggestions?
4
Answers
Or, preferably…
You should do a Select into a temp-table to achieve this.
First, we select all rows with your condition:
this selection gives you a list of post_ids where no banner-entry exists
Now you can use the found post_ids to insert into your table the data you need:
In MySQL 8.3 you can:
meta_key = 'page_type'
andmeta_value = 'city'
,EXCEPT
operation.you can do as follows :
take also the case where if banner exist create page-type