Detailed explanation of MySQL combined query

Detailed explanation of MySQL combined query

Using UNION

Most 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:

  • Return similarly structured data from different tables in a single table query;
  • Execute multiple queries on a single table, returning data as a single query.

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

  • Here is a combination of two statements
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);

  • You can use the following statement
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);

  • Similarly, you can use the WHERE clause
SELECT vend_id, prod_id, prod_price FROM products WHERE prod_price <= 5 OR vend_id IN (1001, 1002);

UNION Rules

  • UNION must consist of two or more SELECT statements, separated by the keyword UNION.
  • Each query in a UNION must contain the same columns, expressions, or aggregate functions (the columns do not need to be listed in the same order).
  • Column data types must be compatible: they do not have to be of the same type, but they must be types that the DBMS can implicitly convert (different numeric types or different date types).

Include or suppress duplicate rows

UNION 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 results

The 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:
  • Detailed explanation of MySQL subqueries (nested queries), join tables, and combined queries
  • MySQL query data by hour, fill in 0 if there is no data
  • Detailed explanation of MySQL sql_mode query and setting
  • MySQL subqueries and grouped queries
  • MySQL grouping queries and aggregate functions
  • Summary of related functions for Mysql query JSON results
  • MySQL query sorting and paging related
  • MySql query time period method
  • Basic multi-table join query tutorial in MySQL
  • Subquery examples in MySQL

<<:  TCP performance tuning implementation principle and process analysis

>>:  Markup language - specify CSS styles for text

Recommend

Sample code for implementing image drawer effect with CSS3

As usual, let’s first post the picture effect: Th...

idea combines docker to realize image packaging and one-click deployment

1. Install Docker on the server yum install docke...

Sharing of web color contrast and harmony techniques

Color contrast and harmony In contrasting conditi...

HTML n ways to achieve alternate color code sample code

This article mainly introduces the sample code of...

A simple example of creating a thin line table in html

Regarding how to create this thin-line table, a s...

What I learned while building my own blog

<br />In one year of blogging, I have person...

MySQL database master-slave configuration tutorial under Windows

The detailed process of configuring the MySQL dat...

How to point the target link of a tag to iframe

Copy code The code is as follows: <iframe id=&...

The submit event of the form does not respond

1. Problem description <br />When JS is use...