INSERT INTO item_quotation
(item_id, quotation_id,name_searched,item_name,other_name,selling_price,discounted_price)
SELECT DISTINCT I.item_id," . $quotation_id . ",T.item_name, I.name,I.other_name, INV.selling_price, I.discounted_price
FROM temp_quotations T, item I, inventory INV<br/>
WHERE (I.name LIKE CONCAT( '%', T.item_name, '%')
OR I.other_name LIKE CONCAT( '%', T.item_name, '%'))
AND INV.item_id = I.item_id;
我有一個表稱爲temp_quotations(temp_item_id,ITEM_NAME)具有值
(1,粉碎機),(2,鑽頭),(3,試劑盒)
我有稱爲項的另一表(item_id,name,other_name,discounted_price)具有值
(1,grinder A,100),(2,kit A,200)
當我執行此sql時,它將值插入表item_quotation(item_id,quotation_id,name_searched ,item_name,other_name,selling_price,discounted_price)例如在這種情況下,它將插入
(1,1,研磨機,研磨機A,150,100)
(2,1,試劑盒,試劑盒A,250,200)
由於項目鑽取在表格項目中找不到,因此不會在表格item_quotations中顯示它。我想在該sql中編寫一條if語句,以便當temp_quotation中的item_name與item中的名稱不匹配時,它不會在item_name的位置找到。 這是我試過,但它並沒有顯示「未找到」:IF在SQL表達式
INSERT INTO item_quotation
(item_id, quotation_id,name_searched,item_name,other_name,selling_price,discounted_price)
SELECT DISTINCT I.item_id," . $quotation_id . ",T.item_name,
IF(I.name LIKE CONCAT( '%', T.item_name, '%'),I.name,'not found'),I.other_name, INV.selling_price, I.discounted_price
FROM temp_quotations T, item I, inventory INV
WHERE (I.name LIKE CONCAT( '%', T.item_name, '%')
OR I.other_name LIKE CONCAT( '%', T.item_name, '%'))
AND INV.item_id = I.item_id;
感謝您的鏈接。 – chupinette 2010-04-04 16:26:54