在Mysql中使用IF THEN转换资金

时间:2016-03-05 00:42:49

标签: mysql

这是我的桌子 enter image description here  我想仅在DOLLAR中选择价格(我的意思是如果AFG单位的价格乘以68) 我试过这个SELECT Price, IF (Unit='AFG' , Price*2) FROM sales但是没有工作

2 个答案:

答案 0 :(得分:2)

试试这个

SELECT Unit, 
    case Unit when 'AFG' then Price * 68 
              else Price end as 'Price'
FROM sales

答案 1 :(得分:1)

IF()是MySQL中的一个函数。函数名IF和参数(condition, if true, if false)

之间没有空格

此外,您的函数缺少第3个参数,即条件为false时函数返回的参数。

更正了查询:

SELECT IF(Unit='AFG', Price*68, Price) As Price 
FROM Sales