Get the App
SLTechnology News&Howtos  ›  Database  › 

Two grammars of case when in MySQL

Shulou Source: shulou.com Published: 2022-06-01 15:42:36 10月01日 Update

In this issue, the editor will bring you two grammars about case when in MySQL, which will be analyzed and described from a professional point of view. I hope you can get something after reading this article.

Usage of Case when: once a WHEN is satisfied, this piece of data exits the CASE WHEN, regardless of other CASE.

There are two syntax for MySQL's case when:

Simple function

CASE [col_name] WHEN [value1] THEN [result1]... ELSE [default] END

Search function

CASE WHEN [expr] THEN [result1]... ELSE [default] END

Example:

1. Search function

CASE WHEN [expr] THEN [result1]... ELSE [default] END: the search function can write judgment, and the search function will only return the first qualified value, and other case will be ignored.

In # when expressions, you can use and to connect conditions SELECT NAME 'hero', age 'age', CASE WHEN age

< 18 THEN '少年' WHEN age < 30 THEN '青年' WHEN age >

= 30 AND age < 50 THEN 'middle age' ELSE 'old age' END 'state' FROM user_info

2. Simple function

CASE [col_name] WHEN [value1] THEN [result1]... ELSE [default] END: enumerates all possible values for this field.

SELECT NAME 'hero', CASE NAME WHEN 'Dreven' THEN 'axe' WHEN 'Demacia-Galen' THEN 'sword' WHEN 'night hunter-VN' THEN' crossbow 'ELSE' no 'END' equipment 'FROM user_info SELECT NAME 'hero', CASE NAME WHEN 'Dreven' THEN 'axe' WHEN 'Demacia-Galen' THEN 'sword' WHEN 'night hunter-VN' THEN' crossbow 'ELSE' no 'END' equipment 'FROM user_info These are the two syntax of case when in MySQL shared by Xiaobian. If you have similar doubts, you might as well try it with reference to the above method. If you want to know more about it, please follow the industry information.

Tags: Function search grammar hero content sword axe condition hunter Delai Galen West Asia professional small and medium field instance juvenile that is age data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MariaDB Microsoft Shulou Tech Info Xiaomi