Home > Database > Mysql Tutorial > body text

How to merge after querying in mysql

WBOY
Release: 2022-01-20 14:50:33
Original
2798 people have browsed it

In mysql, you can use the UNION operator to achieve the effect of merging results after querying. The function of this operator is to connect the results of more than two SELECT statements into a result set. The syntax is "SELECT query Statement 1 UNION [ALL] SELECT query statement 2".

How to merge after querying in mysql

The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.

How does mysql merge after querying

MySQL UNION operator is used to combine the results of two or more SELECT statements into a result set . Multiple SELECT statements will remove duplicate data.

Syntax

MySQL UNION operator syntax format:

SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
UNION [ALL | DISTINCT]
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];
Copy after login

Parameters

  • expression1, expression2, ... expression_n: To Column to retrieve.

  • tables: The data table to be retrieved.

  • WHERE conditions: Optional, search conditions.

  • DISTINCT: Optional, delete duplicate data from the result set. The UNION operator already removes duplicate data by default, so the DISTINCT modifier has no effect on the result.

  • ALL: Optional, returns all result sets, including duplicate data.

The example is as follows:

Query the information of all fruits whose price is less than 9, query the information of all fruits whose s_id is equal to 101 and 103, use UNION connection query result. The SQL statement is as follows:

How to merge after querying in mysql

Recommended learning: mysql video tutorial

The above is the detailed content of How to merge after querying in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!