Select Lastname, Firstnme, Sex,
Case When Sex = F Then ‘female’ Else ‘male’ End
Birthdate From Employee Order by Lastname
这是我的代码,但是 case 语句不会这样工作,我能实现吗?
谢谢
Select Lastname, Firstnme, Sex,
Case When Sex = F Then ‘female’ Else ‘male’ End
Birthdate From Employee Order by Lastname
这是我的代码,但是 case 语句不会这样工作,我能实现吗?
谢谢
将“F”放在引号中:
Select Lastname, Firstnme, Sex,
Case
When Sex = 'F' Then 'female'
When Sex = 'M' Then 'male'
ELSE 'Unknown'
End as Sex_string, Birthdate
From Employee Order by Lastname
或者
Select Lastname, Firstnme, Sex,
Case Sex
When 'F' Then 'female'
When 'M' Then 'male'
ELSE 'Unknown'
End as Sex_string, Birthdate
From Employee Order by Lastname
我相信你想要这样的东西:
select Lastname, Firstnme,
Sex = Case
When Sex = 'F' Then ‘female’
Else ‘male’
End,
Birthdate
From Employee Order by Lastname
这里的区别是你必须设置 Sex = Case,然后在 case 语句后加一个逗号,这样它就知道 Birthdate 是另一个字段。
这是为 SQL 服务器编写的,如果您使用的是 MySql 或其他一些 RDBMS,则您的里程可能会有所不同。
SELECT firstname, lastname, birthdate, sex,
CASE sex
WHEN 'F' THEN 'Female'
WHEN 'M' THEN 'Male'
ELSE 'Unknown'
END AS gender
FROM EMPLOYEE
ORDER BY lastname;
您可以将第二个WHEN
子句从子句中删除,CASE
让ELSE
子句处理任何非女性的内容,但通常有三个性别值 - 男性、女性和未知。上面的脚本占了第三个变量,并将返回所有记录为男性、女性或未知。