当前位置:网站首页>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!
边栏推荐
猜你喜欢
随机推荐
SQL函数 USER
Large and comprehensive pom file example
Gstreamer Plugin注册流程详解
tinymce 如何实现动态国际化
如何解决1045无法登录mysql服务器
els 长条方块变形条件、边界碰撞判定
专访|带着问题去学习,Apache DolphinScheduler 王福政
ttl电平与rs232电平转换电路(232电平定义)
Embedded system driver primary [2] - based on character device driver _ basic framework
C language improvement (3)
打破文件锁限制,以存储力量助力企业增长新动力
数值的整数次方
网络安全第六次作业
栈 && 队列
微信小程序如何实现支付功能?看官方文档头疼(使用云函数的方式操作)「建议收藏」
RKMPP API安装使用总结
供应磷脂-聚乙二醇-羧基,DSPE-PEG-COOH,DSPE-PEG-Acid,MW:5000
rhce第三天作业
未来的金融服务永远不会停歇,牛市仍将继续 2021-05-28
OpenMMLab简介








![[C language] Analysis of function recursion (2)](/img/b1/9baa60a69d41a1823ed92001d1b0a0.png)
