当前位置:网站首页>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!
边栏推荐
猜你喜欢

乐心湖‘s Blog——MySQL入门到精通 —— 囊括 MySQL 入门 以及 SQL 语句优化 —— 索引原理 —— 性能分析 —— 存储引擎特点以及选择 —— 面试题

如何选择正规的期货交易平台开户?

Differences and concepts between software testing and hardware testing

多个驻外使领馆发提醒 事关赴华出行、人身财产安全

rhce第三天作业

拯救流浪猫 | 「喵先锋」系列数字版权盲盒明日开抢

Kunpeng devkit & boostkit

eclipse连接数据库后插入数据报错null

你接受不了60%的暴跌,就没有资格获得6000%的涨幅 2021-05-27

The future of financial services will never stop, and the bull market will continue 2021-05-28
随机推荐
拯救流浪猫 | 「喵先锋」系列数字版权盲盒明日开抢
鲲鹏devkit & boostkit
FFmpeg 的AVCodecContext结构体详解
idea社区版下载安装教程_安装天然气管道的流程
Fabric.js 动态设置字号大小
SQL函数 UCASE
二叉树的类型、构建、遍历、操作
OpenMMLab简介
标量替换、栈上分配、同步消除
目标检测场景SSD-Mobilenetv1-FPN
mysql的case when如何用
GTK:Gdk-CRITICAL **: IA__gdk_draw_pixbuf: assertion ‘GDK_IS_DRAWABLE (drawable)’ failed
Mysql 基本操作指南之mysql查询语句
C# 编译错误:Compiler Error CS1044
删除链表的节点
线程安全问题及关键字synchronized,volatile
网络安全第四次作业
不精确微分/不完全微分(Inexact differential/Imperfect differential)
电脑死机,Word忘了保存怎么办?怎么恢复?(编辑器是WPS)
方舟生存进化淘宝面板服务器是怎么一回事?