今天敲代码的时候,刚好遇到这个问题,写博客记一下。
情况如下:性别字段在数据库中存储的是数字,男对应1,女对应0。然后,查询的时候,我想查询出男和女两个字,而不是1和0。
一开始,我写的sql语句,是直接从查询出性别对应的数字,sql语句如下:
SELECT
Employee.Id,
Employee.Account,
Pwd,
Employee.Name,
Sex,
Email,
Phone,
Position.Name as PositionId,
Team.Name as TeamId,
Store.Name as StoreId,
Enabled
from Employee ,Position,Team, Store
where Employee.PositionId=Position.Id and Employee.TeamId=Team.Id and Employee.StoreId=Store.Id
查询出的结果如下,性别只能查询出数字。
在网友的提示下,我将sql语句修改如下:
SELECT
Employee.Id,
Employee.Account,
Pwd,
Employee.Name,
( CASE Sex
WHEN '1' THEN '男'
WHEN '0' THEN '女'
ELSE ' '
END )as Sex,
Email,
Phone,
Position.Name as PositionId,
Team.Name as TeamId,
Store.Name as StoreId,
Enabled
from Employee ,Position,Team, Store
where Employee.PositionId=Position.Id and Employee.TeamId=Team.Id and Employee.StoreId=Store.Id
在使用了case when(oracle只用decode函数) 之后,查询到的性别终于显示了文字,而不是数字