The task is to keep "mg_brands" table updated with values coming from query on another two tables.
Tables are:
mg_brands | mg_term_taxonomy |mg_terms
----------|-------------------|--------------
term_id | term_taxonomy_id |term_id
name | term_id |name
| taxonomies |
Basically, it works in this way: mg_terms contain records having different categories identified by the "field mg_term_taxonomy.taxonomies".
I would like to extract records having taxonomy "brand" and transfer that data into "mg_brands". The query should be capable of checking existing records updating them or insert new ones if not exist.
For that I am using the below reported query but it create new records, duplicating them instead of doing what I already said.
INSERT into mg_brands
SELECT mg_terms.term_id, mg_terms.name
FROM mg_terms
JOIN mg_term_taxonomy
ON mg_term_taxonomy.term_id=mg_terms.term_id
WHERE mg_term_taxonomy.taxonomy="product_brand"
ON DUPLICATE KEY UPDATE
mg_brands.term_id = mg_terms.term_id,
mg_brands.name= mg_terms.name
Any suggestion why the ON DUPLICATE KEY UPDATE is not doing its work?
Thanks
ON DUPLICATE KEYthat doesn't include theKEYs that the (key)wordKEYin that command is referring to (your indexes on that table) is missing a very important part. So please add them (e.g. as output ofshow create table mg_brands).