mysql - SQL中的IF表达式

标签 mysql conditional-statements

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;

最佳答案

如果您使用 CASE 语句,您将获得所需的输出。

这里是使用链接:

http://msdn.microsoft.com/en-us/library/ms181765.aspx

关于mysql - SQL中的IF表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2575012/

相关文章:

mysql - MySQL 中的数据透视表

条件语句中的 MySQL 子查询

regex - 如何为命名捕获组创建条件正则表达式?

java - 线程通信: How to signal that a key was clicked? java.lang.IllegalMonitorStateException

php - Laravel 4 - 按 MySQL YEAR() 分组和搜索

jquery - 提交按钮在 ajax 生成表的模式弹出 View 中不起作用

PHP MYSQL 问题 : Cannot fetch database information using $resultDetails->fetch_assoc

conditional-statements - 如何在LOGO中使用条件语句

mysql - 通过 NodeJS 访问远程 MySQL (3306) 返回 No Route (113 MySQL)

mysql - twitter 是如何工作的,它是否使用 "in"为关注者选择推文?