当前位置:网站首页>Mysql's case the when you how to use
Mysql's case the when you how to use
2022-08-02 14:01:00 【Yisuyun】
How to use mysql's case when
This article introduces the knowledge of "how to use the case when of mysql". During the operation of the actual case,Many people will encounter such dilemmas, and then let Xiaobian lead you to learn how to deal with these situations!I hope you read it carefully and learn something!
In mysql, "case when" is used to evaluate a list of conditions and return one of multiple possible result expressions; "case when" has two syntax formats: 1. Simple function "CASE[col_name]WHEN[value1]THEN[result1]…ELSE[default]END”; 2. Search function “CASE WHEN[expr]THEN[result1]…ELSE[default]END”.
The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.
How to use mysql's case when
Thecase when statement evaluates a list of conditions and returns one of several possible result expressions.
CASE has two formats: The simple CASE function compares an expression to a set of simple expressions to determine the result.The CASE search function evaluates a set of Boolean expressions to determine the result.Both formats support an optional ELSE parameter.
When doing data analysis, it is necessary to find data from the database and analyze and display it. Accurate data query results are conducive to concise and effective analysis in the later stage.During this period, the case when in mysql was used, and the usage of case when was recorded.
CASE statement is a multi-branch statement structure. The statement first searches for the value equal to the VALUE after CASE from the VALUE after WHEN. If it is found, execute the content of the branch, otherwise execute the content after ELSE.
MySql's case when syntax has two types:
1. Simple function
CASE[col_name]WHEN[value1]THEN[result1]…ELSE[default]END
2. Search function
CASE WHEN[expr]THEN[result1]…ELSE[default]END
Simple function
CASE input_expressionWHEN when_expression THEN result_expression [...n ] [ELSE else_result_expressionEND
Enumerates all possible values of Input_expression.
Input_expression and each when_expression must have the same data type, or be implicitly converted.
Search function
CASE WHEN [Boolean_expression] THEN [result1]…ELSE [default] END
Judging the Boolean_expression expression, if satisfied, return the result after then
Take the personnel table as an example

The first usage, when must be followed by the value of the case column, not an expression
select case sex when sex='1' then 'male' when sex='2' then 'female' else 'other' end as 'gender' from people;select case sex when sex='2' then 'female' when sex='1' then 'male' else 'other' end as 'gender' from people;
Query results

Reason: Because the when is not all possible values of the case column, but an expression, but why does the result of 'male' appear, you need to think about it, and then update it after research and understanding
Correct spelling
select case sex when '1' then 'male' when '2' then 'female' else 'other' end as 'gender' from people;
Query results

"How to use mysql case when" is introduced here, thank you for reading.If you want to know more industry-related knowledge, you can pay attention to the Yisuyun website, and the editor will output more high-quality practical articles for you!
边栏推荐
- 腾讯安全游戏行业研讨会:生态共建,护航游戏产业健康发展
- 二分查找 && 树
- Detailed explanation of stored procedures
- 拯救流浪猫 | 「喵先锋」系列数字版权盲盒明日开抢
- 未来的金融服务永远不会停歇,牛市仍将继续 2021-05-28
- How to do short video food from the media?5 steps to teach you to get started quickly
- Detailed explanation of ORACLE expdp/impdp
- 打破文件锁限制,以存储力量助力企业增长新动力
- CVE-2020-27986(Sonarqube敏感信息泄漏) 漏洞修复
- FreeBSD bnxt以太网驱动源码阅读记录三:
猜你喜欢

此次519暴跌的几点感触 2021-05-21

打破文件锁限制,以存储力量助力企业增长新动力

保姆级教程:写出自己的移动应用和小程序(篇三)

第二届中国Rust开发者大会(RustChinaConf 2021~2022)线上大会正式开启报名

About the development forecast of the market outlook?2021-05-23

【C语言】细品分支结构——switch语句

The world's largest Apache open source foundation is how it works?

电脑死机,Word忘了保存怎么办?怎么恢复?(编辑器是WPS)

数值的整数次方

世界上最大的开源基金会 Apache 是如何运作的?
随机推荐
rhce第三天作业
配置zabbix自动发现和自动注册。
vim复制粘贴_vim如何复制粘贴
static修饰的函数有什么特点(static可以修饰所有的变量吗)
2022-08-02日报:2022年7月最热的10篇AI论文
rust使用mysql插入数据
ORACLE expdp/impdp详解
Kunpeng devkit & boostkit
矩阵中的路径
关于市场后市的发展预测? 2021-05-23
政策利空对行情没有长期影响,牛市仍将继续 2021-05-19
鲲鹏devkit & boostkit
Mysql 基本操作指南之mysql查询语句
【ONE·Data || Getting Started with Sorting】
els long block deformation conditions, boundary collision judgment
A number of embassies and consulates abroad have issued reminders about travel to China, personal and property safety
Break the limit of file locks and use storage power to help enterprises grow new momentum
rpm包的卸载与安装[通俗易懂]
VMM是什么?_兮是什么意思
Image retrieval method based on deep learning!