当前位置:网站首页>mysql case when
mysql case when
2022-07-25 17:36:00 【kankan231】
MySQL中case when的用法:
select f1,case [f2]
when expression1 then value1
when expression2 then value2
else value3
end alias_field
from table;
相当于代码中的:
if(expression1 ){
alias_field = value1;
}elseif(expression2 ){
alias_field = value2;
}else{
alias_field = value3;
}case [f2] 表示f2这个字段是可选的,expression是一个条件表达式或常量,value是一个值,在expression和value中都可以使用字段的值进行判断或运算;
比如一个活动数据表,每个活动都有开始时间和结束时间,在展示时要求将正在进行中的活动排在前面,未开始的排在中间,已结束的排在最后
活动表:activity
id 主键
activity_name 活动名称
start_time 活动开始时间
end_time 活动结束时间
按如上排序规则查询活动数据:
select id,activity_name,
case
when end_time < unix_timestamp(now()) then -1 /*已结束*/
when start_time < unix_timestamp(now()) and end_time > unix_timestamp(now()) then 1 /*进行中*/
else 0 /*未开始*/
end order_number
from activity order by order_number desc limit 1,10;边栏推荐
- 新版selenium4.3在egde浏览器的无头模式
- STM32 PAJ7620U2手势识别模块(IIC通信)程序源码详解
- 8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇
- Update 3dcat real time cloud rendering V2.1.2 release
- 计算日期或日期格式化
- 什么是 IP SSL 证书,如何申请?
- What financial products can you buy to make money with only 1000 yuan?
- Does PgSQL have a useful graphical management tool?
- 【VSCODE】支持argparser/接受命令行参数
- Using rank to discuss the solution of linear equations / the positional relationship of three planes
猜你喜欢
随机推荐
01. Sum of two numbers
关于flickr的数据集笔记
How to fix the first row title when scrolling down in Excel table / WPS table?
Update 3dcat real time cloud rendering V2.1.2 release
03.无重复字符的最长子串
Installation steps and usage of NVM under windows10 system
I want to manage money. I don't understand. Is there a principal guaranteed financial product?
Pymongo saves data in dataframe format (insert_one, insert_many, multi-threaded saving)
Boring post roast about work and life
02.两数相加
服务器端架构设计期末复习知识点总结
OSPF综合实验
交友活动记录
实时黄金交易平台哪个可靠安全?
After consulting about how to deal with DDL in Flink SQL client, how to add fields and jobs to the mapping table in Fink SQL?
【无标题】
Text translation software - text batch translation converter free of charge
函数名指针和函数指针
ROS学习笔记(四)ros 无法rosdep init 或者update解决方法
我也是醉了,Eureka 延迟注册还有这个坑!









