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
Table of contents 1. Nginx installation and start...
Notice! ! ! This situation can actually be avoide...
As usual, let’s first post the picture effect: Th...
Table of contents 1. Phenomenon 2. Solution 3. Su...
1. Install Docker on the server yum install docke...
Color contrast and harmony In contrasting conditi...
This article mainly introduces the sample code of...
Regarding how to create this thin-line table, a s...
1. Overview In the daily operation and maintenanc...
<br />In one year of blogging, I have person...
The detailed process of configuring the MySQL dat...
Step 1: Use Notepad to open the "my.ini"...
The code looks like this: .process{ border:1px so...
Copy code The code is as follows: <iframe id=&...
1. Problem description <br />When JS is use...