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(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,grinder,grinder A,150,100)
(2,1,kit,kit A, 250,200)
由于在表 item 中找不到 item drill,因此不会在表 item_quotations 中显示它。我想在该 sql 中编写一个 if 语句,以便当来自 temp_quotation 的 item_name 与来自 item 的名称不匹配时,它会在 item_name 的位置显示未找到。
这是我尝试过的,但它没有显示“未找到”:
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;
最佳答案
关于mysql - SQL中的IF表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2575012/