当前位置:网站首页>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!
边栏推荐
- MySQL - ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
- 面试SQL语句,学会这些就够了!!!
- The world's largest Apache open source foundation is how it works?
- 期货具体是如何开户的?
- GTK:Gdk-CRITICAL **: IA__gdk_draw_pixbuf: assertion ‘GDK_IS_DRAWABLE (drawable)’ failed
- 【ONE·Data || Getting Started with Sorting】
- 泡利不相容原理适用的空间范围(系统)是多大?
- 【Tensorflow】AttributeError: module ‘keras.backend‘ has no attribute ‘tf‘
- SQL函数 UCASE
- 栈 && 队列
猜你喜欢
线程安全问题及关键字synchronized,volatile
数值的整数次方
【C语言】手撕循环结构 —— for语句
MySQL - ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
The world's largest Apache open source foundation is how it works?
方舟生存进化淘宝面板服务器是怎么一回事?
二进制中1的个数
WeChat Mini Program-Recent Dynamic Scrolling Implementation
供应磷脂-聚乙二醇-羧基,DSPE-PEG-COOH,DSPE-PEG-Acid,MW:5000
数据机构---第六章图---图的遍历---选择题
随机推荐
2022-08-02日报:2022年7月最热的10篇AI论文
政策利空对行情没有长期影响,牛市仍将继续 2021-05-19
[C language] Explicit array solution (1)
Mysql index details (with pictures and texts)
关于Google词向量模型(googlenews-vectors-negative300.bin)的导入问题
Gstreamer Plugin注册流程详解
C# using 使用方法
Interviewer: Can you talk about optimistic locking and pessimistic locking?
标量替换、栈上分配、同步消除
Some impressions of the 519 plummet 2021-05-21
好用的php空间,推荐国内三个优质的免费PHP空间[通俗易懂]
如何解决1045无法登录mysql服务器
LeetCode(剑指 Offer)- 53 - II. 0~n-1中缺失的数字
科研试剂DSPE-PEG-VIP,二硬脂酰基磷脂酰乙醇胺-聚乙二醇-血管活性肠肽VIP
关于C#使用DateTime数据的细节
【C语言】手撕循环结构 —— while语句
期货具体是如何开户的?
【Tensorflow】AttributeError: module ‘keras.backend‘ has no attribute ‘tf‘
VMM是什么?_兮是什么意思
面试官:可以谈谈乐观锁和悲观锁吗