mysql case when then else end 语句

本文详细解析了CASWHENTHENELSEEND语法在SQL中的应用,包括排序、关联不同表及配合聚合函数进行数据统计等场景,为数据库操作提供实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

case when then else end 语法应用场景还是比较多的

1、排序:select stuno,name,math,case math when 90 then ‘优秀’ when 80 then ‘良好’ end as sort from class order by sort desc
2、根据不同字段的值关联不同的表
select left(operation_time,16) as
operation_time,a.operator_id as pc_user_id,a.operator_type,a.operation_content,(case when a.operator_type = 1 then b.pu_name else c.su_name end) as pu_name from object_audit_log as a
left join pc_user as b on
operator_id=pc_user_id
left join system_user as c on
operator_id=system_user_id;
3、配合聚合函数做数据统计
select count(case when att_system_status=0 and att_status=3 and att_fixed=0 and att_rented_status =1 then 1 end ) as count_publishing,count(case when att_fixed then 1 end ) as count_fixed,count(case when att_system_status=0 and att_status=2 and att_fixed=0 and att_rented_status =1 then 1 end ) as count_auditing,count(case when att_system_status=1 and att_status=3 and att_fixed=0 and att_rented_status =1 then 1 end ) as count_canclepublish,count(case when att_status=0 and att_fixed=0 and att_rented_status =1 then 1 end ) as count_nosubmit from property a left join apartment on a.property_id = att_property_id left join property_company on a.ppt_property_company_id = property_company_id left join city on a.ppt_city_id = city_id;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值