当前位置:网站首页>mysql case when
mysql case when
2022-07-25 17:39:00 【kankan231】
MySQL in case when Usage of :
select f1,case [f2]
when expression1 then value1
when expression2 then value2
else value3
end alias_field
from table;
Equivalent to... In the code :
if(expression1 ){
alias_field = value1;
}elseif(expression2 ){
alias_field = value2;
}else{
alias_field = value3;
}case [f2] Express f2 This field is optional ,expression Is a conditional expression or constant ,value It's a value , stay expression and value You can use the value of the field for judgment or operation ;
For example, an activity data table , Each activity has a start time and an end time , During the presentation, it is required to rank the activities in progress first , Those that have not started are in the middle , The finished ones are at the bottom
Activity list :activity
id Primary key
activity_name The name of the event
start_time Activity start time
end_time End time
Query activity data according to the above sorting rules :
select id,activity_name,
case
when end_time < unix_timestamp(now()) then -1 /* Has ended */
when start_time < unix_timestamp(now()) and end_time > unix_timestamp(now()) then 1 /* Have in hand */
else 0 /* Not yet begun */
end order_number
from activity order by order_number desc limit 1,10;边栏推荐
- [Hardware Engineer] Why do DC-DC isolated switching power modules use transformers?
- Three dimensional function display of gray image
- window10系统下nvm的安装步骤以及使用方法
- 【无标题】
- OSPF综合实验
- 关于flickr的数据集笔记
- The gas is exhausted! After 23 years of operation, the former "largest e-commerce website in China" has become yellow...
- 一篇文章了解超声波加湿器
- HCIP第一天实验
- MySQL数据库中去重与连接查询的方法
猜你喜欢

Idea essential plug-ins

Chapter VI succession

Three dimensional function display of gray image

Using rank to discuss the solution of linear equations / the positional relationship of three planes

stm32F407------SPI
![[Hardware Engineer] can't select components?](/img/bd/fdf62b85c082f7e51bf44737f1f787.png)
[Hardware Engineer] can't select components?

OSPF---开放式最短优先路径协议

With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development

WPF 实现用户头像选择器

Technical difficulties and applications of large humanoid robots
随机推荐
ROS学习笔记(四)ros 无法rosdep init 或者update解决方法
EasyUI modification and DataGrid dialog form control use
实时黄金交易平台哪个可靠安全?
I'm also drunk. Eureka delayed registration and this pit!
Interviewer: talk about log The difference between fatal and panic
WPF 实现用户头像选择器
Using rank to discuss the solution of linear equations / the positional relationship of three planes
Summary of 80 domestic database operation documents (including tidb, Damon, opengauss, etc.)
Chapter 4: operators
Stm32 paj7620u2 gesture recognition module (IIC communication) program source code explanation
Product life cycle to be considered in making intelligent hardware
[cadence Allegro PCB design] permanently modify the shortcut key (customized) ~ it is valid for personal test~
[Hardware Engineer] can't select components?
HCIP第一天实验
Enumeration classes and magic values
Which one of the electronic products has a longer service life??
[cadence Allegro PCB design] error: possible pin type conflict gnd/vcc power connected to output
我也是醉了,Eureka 延迟注册还有这个坑!
Redis源码与设计剖析 -- 18.Redis网络连接库分析
Cross validation (CV) learning notes