Using UNIONMost SQL queries consist of a single SELECT statement that returns data from one or more tables. MySQL also allows you to execute multiple queries (multiple SELECT statements) and return the results as a single query result set. These combined queries are often called unions. There are two situations where combined queries are required:
Combining queries and multiple WHERE conditions: In most cases, combining two queries on the same table accomplishes the same work as a single query with multiple WHERE clause conditions. You can use the UNION operator to combine several SQL queries. All you need to do is give each SELECT statement and put the keyword UNION between each statement. For example: Suppose we need a list of all items with a price less than or equal to 5, and we also want to include all items produced by suppliers 1001 and 1002. Of course, we can use the WHERE clause to accomplish this, but here we use UNION
SELECT vend_id, prod_id, prod_price FROM products WHERE prod_price <= 5; SELECT vend_id, prod_id, prod_price FROM products WHERE vend_id IN (1001, 1002);
SELECT vend_id, prod_id, prod_price FROM products WHERE prod_price <= 5 UNION SELECT vend_id, prod_id, prod_price FROM products WHERE vend_id IN (1001, 1002);
SELECT vend_id, prod_id, prod_price FROM products WHERE prod_price <= 5 OR vend_id IN (1001, 1002); UNION Rules
Include or suppress duplicate rowsUNION will automatically remove duplicate rows from the query result set. If you want to return all matching rows, use UNION ALL. SELECT vend_id, prod_id, prod_price FROM products WHERE prod_price <= 5 UNION ALL SELECT vend_id, prod_id, prod_price FROM products WHERE vend_id IN (1001, 1002); Sorting combined query resultsThe output of the SELECT statement is sorted using the ORDER BY clause. When using UNION to combine queries, only one ORDER BY clause can be used. It must appear after the last SELECT statement. Multiple ORDER BY clauses are not allowed. SELECT vend_id, prod_id, prod_price FROM products WHERE prod_price <= 5 UNION SELECT vend_id, prod_id, prod_price FROM products WHERE vend_id IN (1001, 1002) ORDER BY vend_id, prod_price; The above is a detailed explanation of MySQL combined query. For more information about MySQL combined query, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: TCP performance tuning implementation principle and process analysis
>>: Markup language - specify CSS styles for text
The scope of css is global. As the project gets b...
Table of contents 1.1. Network access between con...
1. Check whether MySQL is installed yum list inst...
This article refers to the work of 51CTO blog aut...
Table of contents Design scenario Technical Point...
Say it in advance On a whim, I want to know what ...
This article shares the specific code of jquery t...
The Riddle vulnerability targeting MySQL versions...
When you feel that there is a problem with MySQL ...
In the previous blog, Xiao Xiong updated the meth...
Table of contents What to do when registering an ...
When exporting data to operations, it is inevitab...
Table of contents Business Background Using Techn...
1: Tag selector The tag selector is used for all ...
1. Download jdk download address我下載的是jdk-8u221-li...