SQL语句之 case....when...then 语句
1.用简单的sql语句
- SELECT dbo.tb_userinfo.user_id, dbo.tb_userinfo.user_name, dbo.tb_role.role_name, dbo.tb_userinfo.sex, dbo.tb_userinfo.phone, dbo.tb_userinfo.password,
- dbo.tb_userinfo.real_name, dbo.tb_userinfo.email, dbo.tb_userinfo.default1, dbo.tb_userinfo.default2
- FROM dbo.tb_userinfo INNER JOIN
- dbo.tb_role ON dbo.tb_userinfo.role_id = dbo.tb_role.role_id
- WHERE (dbo.tb_userinfo.record_status = 1) AND (dbo.tb_role.record_status = 1)
结果:
2.用含case语句的sql语句
- SELECT dbo.tb_userinfo.user_id AS 用户编号, dbo.tb_userinfo.user_name AS 用户名, dbo.tb_userinfo.password AS 登陆密码,
- dbo.tb_userinfo.real_name AS 真实姓名, dbo.tb_role.role_name AS 角色名称, dbo.tb_userinfo.sex AS 性别, dbo.tb_userinfo.phone AS 联系电话,
- dbo.tb_userinfo.email AS 电子邮件,
- CASE WHEN dbo.tb_userinfo.record_status = 1 THEN '使用' WHEN dbo.tb_userinfo.record_status = 2 THEN '注册' WHEN dbo.tb_userinfo.record_status
- = 0 THEN '删除' END AS 用户状态
- FROM dbo.tb_userinfo INNER JOIN
- dbo.tb_role ON dbo.tb_userinfo.role_id = dbo.tb_role.role_id
- WHERE (dbo.tb_role.record_status = 1)
运行结果: