if语句,修改输出

时间:2012-03-16 01:10:57

标签: mysql database if-statement

此方案的SQL语句是什么?

tbl_user_account

id INT(11)
name varchar(100)
username varchar(25)
password varchar(25)
active int(1)


_____________________________________
|id|name | username|password  |active|
_____________________________________
|1 |John | jhon123 | p@ssw0rd | 1  | 
|2 |Maria| maria321| p@ss     | 0  |

我想获取所有记录,但必须输出活动字段 如果是1则为“是”,如果为0则为“否”

我想要这个输出:


|id|name | username|password  |active|
_____________________________________
|1 |John | jhon123 | p@ssw0rd | Yes  | 
|2 |Maria| maria321| p@ss     | No  |

我是否需要创建一个函数或只是一个if语句?

2 个答案:

答案 0 :(得分:1)

试试这个:

select id, name, username, password, if (active >0, "YES", "NO") from table_name

答案 1 :(得分:1)

SELECT id, name, username, password,
  CASE active
    WHEN 0 THEN "no"
    WHEN 1 THEN "yes"
    ELSE "Umm" 
  END 
FROM tbl_user_account