如何条件然后在mysql中相乘?

时间:2016-02-29 11:40:34

标签: mysql

我收到此错误消息! 1064 - 您的SQL语法出错;检查与您的MySQL服务器版本对应的手册,以便在'=='07'||附近使用正确的语法SUBSTRING(col2,0,3)== '447') billsec * .06作为第2行的费率

SELECT col1, col2, col3, col4, 
IF (SUBSTRING(col2, 0, 2) == '07' || SUBSTRING(col2, 0, 3) == '447') 
col4 * .06 as col5;
ELSE 
col4 * .03 as col5;
FROM table_name WHERE id='123'

但查询只是这个

 SELECT col1, col2, col3, col4, 
    col4 * .06 as col5
    FROM table_name WHERE id='123'

它不会返回任何错误..有人可以告诉我如何管理它吗?

1 个答案:

答案 0 :(得分:1)

试试这个

SELECT col1, col2, col3, col4,
IF(SUBSTRING(col2, 1, 2) = '07' OR SUBSTRING(col2, 1, 3) = '447' , col4 * .06  , col4 * .03  ) AS col5
FROM table_name WHERE id='123';
在mysql中还有另一个版本的if ... else IF(CONDITION ,IF_CONTDITION_TRUE, ELSE_PART)

SQLFiddle