Home >Database >Mysql Tutorial >What is the usage of in in the select statement of mysql?

What is the usage of in in the select statement of mysql?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2022-04-06 15:51:3013602browse

In mysql, in in the select statement is often used in the where expression. Its function is to query data within a certain range. The range of data queried by in is not necessarily clear and may contain subquery statements. , the syntax is "select * from where...in (range data or subquery statement)".

What is the usage of in in the select statement of mysql?

The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.

What is the usage of in in the select statement of mysql

1. Basic usage

in in mysql is often used in where expressions, and its function It is to query data within a certain range.

select * from where field in (value1,value2,value3,…)

When IN is preceded by the NOT operator, it means the opposite meaning to IN, that is, not to select within these list items

select * from where field not in (value1,value2,value3,…)

2. IN subquery

In more cases, the value of the IN list item is ambiguous and may be obtained through a subquery:

SELECT * FROM article WHERE uid IN(SELECT uid FROM user WHERE status=0)

In this SQL example, we implement the method of finding out all states All posts by 0 users (probably banned). First, get all users with status=0 through a query:

SELECT uid FROM user WHERE status=0

Then use the query result as a list item of IN to achieve the final query result. Note that the result returned in the subquery must be a field list item .

Example: Query all data with IDs 2 and 4 in the book table:

There is a book data table with the following content:

What is the usage of in in the select statement of mysql?

The query statement and query results are shown in the following diagram:

What is the usage of in in the select statement of mysql?

Recommended learning: mysql video tutorial

The above is the detailed content of What is the usage of in in the select statement of mysql?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn