MySql Error::error in sql syntax

MySql Error::error in sql syntax

MySql查询如下:

SELECT Name, 
SUM(Balance) AS Balance, 
(IF (Balance<=0), abs(Balance), 0 ) AS Exc, 
(IF (Balance>=0), Balance, 0 ) AS Del  
from table1 group by name

Mysql 错误:

You have an error in your sql syntax; check manual that corresponds to your MySql server version for the right syntax to use near

你可以像这样使用 CASE ... WHEN 条件语句

    SELECT Name, 
    SUM(Balance) AS Balance,
    (CASE WHEN SUM(Balance)>=0 THEN SUM(Balance) WHEN SUM(Balance)<0 THEN SUM(Balance) * -1 END) AS Balance_Positive
    from table1 group by name
SELECT Name, 
SUM(Balance) AS Balance, 
IF( (SUM(Balance)<=0), abs(SUM(Balance)), 0 ) AS Exc, 
IF( (SUM(Balance)>=0),SUM(Balance), 0 ) AS Del  
from table1 group by Name