Home  >  Article  >  Database  >  What is the mysql union query statement?

What is the mysql union query statement?

coldplay.xixi
coldplay.xixiOriginal
2021-01-06 10:43:513421browse

The mysql union query statement is [query statement 1 union query statement 2]. It is required that the type of each column in the query of multiple query statements should be consistent. The union keyword defaults to deduplication. If you use union all, you can Contains duplicates.

What is the mysql union query statement?

The operating environment of this tutorial: Windows 7 system, mysql version 8.0.22, DELL G3 computer. This method is suitable for all brands of computers.

Related free learning recommendations: mysql video tutorial

Union Query

UNION Union Merge: Combine the results of multiple query statements into one result

Syntax:

查询语句1
union
查询语句2

Application scenarios:

The results to be queried come from multiple tables, and the multiple tables have no direct connection relationship, but the queried information is consistent

Features:

1. The number of query columns in multiple query statements is required to be consistent

2. Each query in multiple query statements is required to be consistent. The type of a column should be consistent with the best

3. The union keyword is deduplicated by default. If union all is used, duplicate items can be included

Introduced cases: Query department number > 90 or the email address contains a Employee information

SELECT * FROM employees
WHERE email like '%a%'
or department_id >90
SELECT * FROM employees
WHERE email like '%a%'
UNION
SELECT * FROM employees
where department_id >90;

Case: Query the information of males among Chinese users and the information of males among foreign users

SELECT id,cname,csex
FROM t_ca
where csex='男'
UNION all
SELECT t_id,tName
FROM t_ua
where  tGender='male';

The above is the detailed content of What is the mysql union query statement?. For more information, please follow other related articles on the PHP Chinese website!

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