2015-11-23 46 views
-1

我有一個項目SQL查詢來插入/更新根據條件(無法插入)的數量

ItemID ItemName 
1  a   
2  b 

組件表

cid name rate 
12 ff  2 
13 tt  4 
14 ff  4 
15 ff  2 

compenent_item

pk_id cid itemid qty 
1   12  1   3 
2   13  1   4 
3   14  1   4 
4   15  1   4 
5   12  2   3 
6   13  2   4 
7   14  2   4 
8   15  2   4 

表如果itemid和cid不存在於我的組件表中,我必須根據情況在我的component_item表中插入。

我無法作出正確的查詢

+2

添加您的嘗試! –

+3

另外,標記您正在使用的DBMS。 – Siyual

+0

你的語言有點模棱兩可。如果component_item中沒有行,這是否意味着您想要將「Item」和「Component」的交叉積插入它? (在你的例子中是8行)?隨着組件和項目的增長,這可能會使未來的更新非常昂貴。 –

回答

0

只是我不知道你想要什麼數量價值...

Insert compenent_item(cid, itemId, qty) 
Select i.ItemId, c.cid, ?? 
from Item I 
    full join component c 
where not exists(Select * from compenent_item 
       where cid = c.cid 
        and itemId = i.ItemID)