Add rows to a table for each ID from a result-set
Consider the result-set
SELECT UNIQUE order_id FROM 开发者_JS百科order_details WHERE date=2011-01-01
(The PK is order_id and item_id together.)
For each order_id in this result-set, I want to append a gift (which has an item_id = 1000027) to that id - meaning I want to add an additional row in the order_details table for each order_id from this result_set (since each order from that day gets a gift). I will 'hardcode' 1000027 for each inserted row.
Something like this should do the trick. Please note I am making some assumptions on your columns. I have no idea what other columns exist in the table however I'm pretty sure that order_id, item_id and date are required.
INSERT INTO order_details (order_id, item_id, date)
SELECT UNIQUE
order_id,
1000027,
getDate()
FROM order_details
WHERE date = '01/01/2011'
AND item_id <> 1000027
精彩评论