当前位置:网站首页>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;边栏推荐
- Excel表格 / WPS表格中怎么在下拉滚动时让第一行标题固定住?
- An article about ultrasonic humidifier
- Headless mode of new selenium4.3 in egde browser
- What financial products can you buy to make money with only 1000 yuan?
- Lvgl 7.11 tileview interface cycle switching
- 【硬件工程师】元器件选型都不会?
- [cadence Allegro PCB design] error: possible pin type conflict gnd/vcc power connected to output
- Box selection screenshot shortcut key of win10
- 我想理财,不懂,有没有保本金的理财产品?
- POWERBOARD coco! Dino: let target detection embrace transformer
猜你喜欢

如何看一本书

EasyUI modification and DataGrid dialog form control use

stm32F407------SPI

EasyUI drop-down box, add and put on and off shelves of products

我也是醉了,Eureka 延迟注册还有这个坑!

【硬件工程师】元器件选型都不会?

Idea 必备插件

HCIP第一天实验
![[knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part3): rule-based problem classification](/img/4c/aeebbc9698f8d5c23ed6473c9aca34.png)
[knowledge atlas] practice -- Practice of question answering system based on medical knowledge atlas (Part3): rule-based problem classification

Virtual memory management
随机推荐
Beyond convnext, replknet | look 51 × 51 convolution kernel how to break ten thousand volumes!
Trooper
实时黄金交易平台哪个可靠安全?
01.两数之和
Outlook tutorial, how to search for calendar items in outlook?
Tkinter module advanced operations (I) -- transparent buttons, transparent text boxes, custom buttons and custom text boxes
Enumeration classes and magic values
Stm32 paj7620u2 gesture recognition module (IIC communication) program source code explanation
Chapter VI succession
更新|3DCAT实时云渲染 v2.1.2版本全新发布
食品安全 | 八问八答带你重新认识小龙虾!这样吃才对!
做智能硬件要考虑的产品生命周期
[Hardware Engineer] can't select components?
电子产品“使用”和“放置”哪个寿命更长??
Add batch delete
[cadence Allegro PCB design] error: possible pin type conflict gnd/vcc power connected to output
Does PgSQL have a useful graphical management tool?
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
【Cadence Allegro PCB设计】error: Possible pin type conflict GND/VCC Power Connected to Output
博后招募 | 西湖大学机器智能实验室招聘博士后/助理研究员/科研助理