Mysql 'On Duplicate Key INSERT .... SELECT...'
Posted
by calumbrodie
on Stack Overflow
See other posts from Stack Overflow
or by calumbrodie
Published on 2010-03-28T16:17:15Z
Indexed on
2010/03/28
16:23 UTC
Read the original article
Hit count: 273
Hi, I'm looking for a way to do the following in mysql.
INSERT INTO category (cat_id,sku,description,color)
VALUES ('$cat_id','$sku','$description','$color')
ON DUPLICATE KEY UPDATE description=$description"
this works fine... but I want something further to happen if there is a duplicate key. I want to copy a row from the 'product' table to the 'categorized_products'...
AND INSERT categorized_products (a,b,c) SELECT a,b,c FROM products
WHERE products.cat = '$cat_id'
is there a way to do the INSERT...SELECT as part of the first query or do I need to evaluate the above with mysql_affected_rows ( >1 is updated and == 1 is inserted) and then run my second query.
Obviously the above will work but means another query and more LOC. Thanks!
© Stack Overflow or respective owner