Home  >  Article  >  Database  >  What is the usage of not exists in mysql

What is the usage of not exists in mysql

WBOY
WBOYOriginal
2022-03-01 14:39:2018258browse

In mysql, "not exists" means non-existence. The condition is judged after where, followed by the subquery statement. The judgment is made based on whether the subquery statement has a result. The syntax is "Select*from TableA a where Not Exists (subquery statement)".

What is the usage of not exists in mysql

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

What is the usage of not exists in mysql

Here we focus on Not Exists (Exists usage is similar), Not Exists does not exist

Usage:

Select  * from TableA  a  where Not Exists (Select * from TableB  b where a.id=b.id and a.name=b.name);

1. Not Exists is used after where, and is followed by the subquery statement (with parentheses);

2. Not Exists(Exists) does not care about the specific result of the subquery, only cares about it. Does the subquery have any results?

3. The meaning of this statement is to substitute the records of TableA into the subquery one by one. If the subquery result set is empty, it means that it does not exist, then this record of TableA appears in The final result set, otherwise it will be excluded;

Usage:

Select * from TableA a where Not Exists (Select 1 from TableB);

The subquery of this statement is not empty under any circumstances, causing the final result set to be empty, because every item in TableA The subqueries corresponding to the records all have result sets, indicating that they all exist, so the final result set is empty;

Recommended learning: mysql video tutorial

The above is the detailed content of What is the usage of not exists in 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