not exists usage

Apr 26, 2019 am 09:37 AM
sql

not exists is a syntax in SQL. It is commonly used between subqueries and main queries. It is used for conditional judgment. It returns a Boolean value based on a condition to determine how to proceed with the next operation. The same is true for not exists. The opposite of exists or in.

not exists usage

Not exists is the opposite of exists, so to understand the usage of not exists, we first understand the differences and characteristics of exists and in:

exists: The emphasis is on whether to return the result set, without knowing what to return. For example:

select name from student where sex = 'm' and mark exists(select 1 from grade where ...)
Copy after login

As long as the exists guide clause returns the result set, then the exists condition is established. Please pay attention to the fields returned. It is always 1. If it is changed to "select 2 from grade where...", then the returned field is 2, and this number is meaningless. So the exists clause does not care about what is returned, but whether there is a result set returned.

The biggest difference between exists and in is that the in clause can only return one field, for example:

select name from student where sex = 'm' and mark in (select 1,2,3 from grade where ...)
Copy after login

The in clause returns three fields, which is incorrect, exists The clause is allowed, but in only allows one field to be returned. Just remove any two fields in 1, 2, and 3.

Not exists and not in are the opposites of exists and in respectively.

exists     (sql       返回结果集,为真)
Copy after login

Mainly depends on whether the sql statement result in exists brackets has a result. If there is a result: the where condition will continue to be executed; if there is no result: it is considered that the where condition is not established.

not exists   (sql       不返回结果集,为真)
Copy after login

Mainly depends on whether the sql statement in the not exists brackets has a result. If there is no result: the where condition will continue to be executed; if there is a result: the where condition is deemed not to be true.

not exists: After testing, when the subquery and the main query have related conditions, it is equivalent to removing the subquery data from the main query.

not exists usage

For example:

test data: id name

1 1 Zhang San

2 李四

select * from test c where  not exists
(select 1 from test t where t.id= '1' )
--无结果
Copy after login
select * from test c where  not exists
(select 1 from test t where t.id= '1'  and t.id = c.id)
--返回2 李四
Copy after login

The above is the detailed content of not exists usage. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

What is the difference between HQL and SQL in Hibernate framework?

Usage of division operation in Oracle SQL Usage of division operation in Oracle SQL Mar 10, 2024 pm 03:06 PM

Usage of division operation in Oracle SQL

What does the identity attribute in SQL mean? What does the identity attribute in SQL mean? Feb 19, 2024 am 11:24 AM

What does the identity attribute in SQL mean?

Comparison and differences of SQL syntax between Oracle and DB2 Comparison and differences of SQL syntax between Oracle and DB2 Mar 11, 2024 pm 12:09 PM

Comparison and differences of SQL syntax between Oracle and DB2

How does Java use the MySQL driver interceptor to implement SQL time-consuming calculations? How does Java use the MySQL driver interceptor to implement SQL time-consuming calculations? May 27, 2023 pm 01:10 PM

How does Java use the MySQL driver interceptor to implement SQL time-consuming calculations?

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Feb 26, 2024 pm 07:48 PM

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags

How SpringBoot encrypts the SQL account password of the configuration file How SpringBoot encrypts the SQL account password of the configuration file May 22, 2023 pm 08:50 PM

How SpringBoot encrypts the SQL account password of the configuration file

How to solve the 5120 error in SQL How to solve the 5120 error in SQL Mar 06, 2024 pm 04:33 PM

How to solve the 5120 error in SQL

See all articles