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
I used js to create a package for converting Chin...
Table of contents Preface 1. Basic knowledge of d...
1. Use contrasting colours. The contrast here ref...
Summarize 1. Similarities Both can change the int...
I'm currently learning about front-end perform...
Prerequisites Compose is a tool for orchestrating...
drop table Drop directly deletes table informatio...
Table of contents 1. Introduction 2. Usage Statel...
1. Download cuda10.1: NVIDIA official website lin...
1. Upload rz to the server and decompress it rz [...
This tutorial shares the process of manually inst...
Run the command: glxinfo | grep rendering If the ...
8 optimization methods for MySQL database design,...
HTML tag: superscript In HTML, the <sup> tag...
As shown below: update table1 as z left join tabl...