Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of MYSQL subquery and merge query

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Subquery subquery with IN keyword

The comparison operator applies only if the result column returned by the subquery contains a value. If the result set returned by a subquery is a list of values, the comparison operator must be replaced by the IN operator.

The IN operator can detect whether a specific value exists in the result set and execute an external query if the test is successful.

Example 1:

View the score information corresponding to the qualified contents of the score field in the info table

SELECT * from info where score in (SELECT score FROM infos)

Query results:

Example 2:

View the same celebrity information in infos table and info table

SELECT * from info where name in (SELECT name FROM infos)

Query results:

Subquery with comparison operator

Subqueries can use comparison operators. These comparison operators include =,! =, >, > =, ANY (SELECT age from infos)

Query results:

Subquery with ALL keyword

The ALL keyword indicates that all conditions are met. When using the ALL keyword, the outer query statement can be executed only if all the results returned by the inner query statement are satisfied.

Example:

SELECT * from info where age < all (SELECT age from infos)

Query results:

Merge query

To merge query results is to merge the query results of multiple SELECT statements together. Merge query results using the UNION and UNION ALL keywords.

Example 1:

SELECT score from info UNION SELECT score from infos

Query results:

Example 2:

SELECT score from info UNION SELECT name from infos

Query results:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report