在MySQL查询问题中获取SQL语法错误

时间:2015-01-15 09:52:49

标签: mysql

这是我的疑问:

SELECT id, `name`, active, username, `function`, email ,
( IF active = '1' THEN SET active = 'Active') AS active ELSEIF  active = '0' SET active = 'Inactive' ELSEIF active IS NULL SET active = '-') AS active

FROM users ORDER BY id DESC;

我想检查一下是否为1然后将其设置为“有效”,否则......如果......我做错了什么?

更新错误消息:

  You have an error in your SQL syntax; check the manual that corresponds to your MySQL
 server version for the right syntax to use near 'active == '1' THEN SET active = 
'Active') as active ELSEIF  active == '0' SET ac' at line 2

1 个答案:

答案 0 :(得分:0)

if条件没有这样的语法。您可以使用case

SELECT id, `name`, active, username, `function`, email,
       case when active = '1' then 'Active'
            when active = '0' then 'Inactive'
            when active is null then '-'
       end AS active_value
FROM users 
ORDER BY id DESC;

并且您不能拥有2个具有相同名称的列。我将案例命名为active_value