开发者

Add rows to a table for each ID from a result-set

开发者 https://www.devze.com 2023-03-11 20:36 出处:网络
Consider the result-set SELECT UNIQUE order_id FROM 开发者_JS百科order_details WHERE date=2011-01-01

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号