Summary of methods for writing judgment statements in MySQL

Summary of methods for writing judgment statements in MySQL

How to write judgment statements in mysql:

Method 1. CASE function

case function syntax:

CASE condition
WHEN value1 THEN returnvalue1
WHEN value2 THEN returnvalue2
WHEN value3 THEN returnvalue3
…
ELSE defaultvalue
END

Example:

SELECT username,(CASE sex WHEN 1 THEN 'male' WHEN 2 THEN 'female' ELSE 'unknown' END) as sex FROM user;

The query results are as follows:

e08a6ceb7c5f565e980547c90d0981c.png

Method 2. IF() function

The CASE function can implement very complex logical judgments. If it implements a simple judgment logic like A if the condition is met, and B otherwise, the CASE function will appear bloated. MYSQL provides the IF() function to simplify this kind of logical judgment. Its syntax format is as follows: IF(condition,A,B)

If condition is true, return A, otherwise return B. So if there are multiple conditions, you cannot use the IF function. IF() returns a number or a string.

select username,if(sex=1,'男','女') as sex from user;

The query results are as follows:

57558ec379007199e539b0a2a063727.png

As you can see, the username zhangsan is displayed as female because our condition is that sex=1 is male, and the others are female. Therefore, the IF function is used to judge only two situations. MySQL also has an IFNULL(value1, value2) function, which is generally used to replace NULL values. We know that NULL values ​​cannot participate in numerical operations.

Method 3. String operation ELT()

grammar:

ELT(N,str1,str2,str3,...)

If N = 1, return str1, if N = 2, return str2, and so on. If N is less than 1 or greater than the number of arguments, returns NULL.

SELECT username,ELT(sex,'男','女','未知') as sex FROM user

The query results are as follows:

2cb2e8899c8d85f8f8c7feeb24b90ae.png

Content extension:

mysql conditional statement

In business scenarios, for example, a user has several states (1: valid, 2: invalid, 3: other), and conditional statements are needed for queries.

grammar:

CASE field WHEN `condition 1` THEN `result 1`
 WHEN `condition 2` THEN `result 2`
 WHEN ...
 ELSE `Other results`
END

Example:

SELECT id,name,
(CASE status
	WHEN 1 THEN 'valid' 
	WHEN 2 THEN 'Invalid' 
ELSE 'Other' 
END) AS status 
FROM user

This is the end of this article summarizing the methods of writing judgment statements in MySQL. For more information about how to write judgment statements in MySQL, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL uses show status to view MySQL server status information
  • How to get table information in MySQL show table status
  • Summary of commonly used SQL statements for creating MySQL tables
  • Record a pitfall of MySQL update statement update
  • Navicat Premium operates MySQL database (executes sql statements)
  • The difference between two MySQL delete user statements (delete user and drop user)
  • Summary of MySQL database like statement wildcard fuzzy query
  • MySQL data duplicate checking and deduplication implementation statements
  • Summary of MySQL's commonly used concatenation statements
  • Use of MySQL SHOW STATUS statement

<<:  Basic usage of custom directives in Vue

>>:  Docker image cannot be deleted Error: No such image: xxxxxx solution

Recommend

Vue3+TypeScript implements a complete example of a recursive menu component

Table of contents Preface need accomplish First R...

Use of MySQL DDL statements

Preface The language classification of SQL mainly...

Native JS music player

This article example shares the specific code of ...

Detailed analysis of each stage of nginx's http request processing

When writing the HTTP module of nginx, it is nece...

How to change the color of the entire row (tr) when the mouse stops in HTML

Use pure CSS to change the background color of a ...

Vue keeps the user logged in (various token storage methods)

Table of contents How to set cookies Disadvantage...

Html and CSS Basics (Must Read)

(1) HTML: HyperText Markup Language, which mainly...

Docker beginners' first exploration of common commands practice records

Before officially using Docker, let's first f...

Refs and Ref Details in Vue3

The editor also shares with you the corresponding...

Two ways to prohibit clearing the input text input cache in html

Most browsers will cache input values ​​by defaul...

How to install mysql5.7.24 binary version on Centos 7 and how to solve it

MySQL binary installation method Download mysql h...

Master-slave synchronization configuration of Mysql database

Table of contents Mysql master-slave synchronizat...

JS Easy to understand Function and Constructor

Table of contents 1. Overview 1.1 Creating a func...

HTML hyperlinks explained in detail

Hyperlink Hyperlinks are the most frequently used ...

36 principles of MySQL database development (summary)

Preface These principles are summarized from actu...