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

js implements a simple English-Chinese dictionary

This article shares the specific code of js to im...

Implementation of drawing audio waveform with wavesurfer.js

1. View the renderings Select forward: Select bac...

10 Underused or Misunderstood HTML Tags

Here are 10 HTML tags that are underused or misun...

Detailed explanation of MySQL table name case-insensitive configuration method

By default, MySQL in Linux distinguishes between ...

Detailed tutorial on installing phpMyAdmin on Ubuntu 18.04

We will install phpMyAdmin to work with Apache on...

Implementation of fuzzy query like%% in MySQL

1, %: represents any 0 or more characters. It can...

Summary of various methods for JS data type detection

Table of contents background What are the methods...

Implementation of services in docker accessing host services

Table of contents 1. Scenario 2. Solution 3. Conc...

A solution to the abnormal exit of Tomcat caused by semaphore

I'm playing with big data recently. A friend ...

MySQL series of experience summary and analysis tutorials on NUll values

Table of contents 1. Test Data 2. The inconvenien...

How to display TIF format images in browser

The browser displays TIF format images Copy code T...

30 excellent examples of color matching in web design

Today, this article has collected 30 excellent cas...

Do you know all 24 methods of JavaScript loop traversal?

Table of contents Preface 1. Array traversal meth...

WeChat Mini Program implements the likes service

This article shares the specific code for the WeC...