Relational database (7): UNION and JOIN operators in MySQL

UNION operator

describe

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

grammar

MySQL UNION 操作符语法格式:

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

parameter

  • expression1, expression2, ... expression_n : Columns to retrieve.

  • tables:  The tables of data to retrieve.

  • WHERE conditions:  optional, retrieval conditions.

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

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


UNION instance

# 下面的 SQL 语句从 "Websites" 和 "apps" 表中选取所有不同的country(只有不同的值):
SELECT country FROM Websites
UNION
SELECT country FROM apps
ORDER BY country;

Note: UNION cannot be used to list all countries in both tables. If some websites and apps are from the same country, each country will only be listed once. UNION just picks distinct values. Use UNION ALL to pick duplicate values!


UNION ALL instance

# 使用 UNION ALL 从 "Websites" 和 "apps" 表中选取所有的country(包含重复的值):
SELECT country FROM Websites
UNION ALL
SELECT country FROM apps
ORDER BY country;

# 带有 WHERE 的 SQL UNION ALL
使用 UNION ALL 从 "Websites" 和 "apps" 表中选取所有的中国(CN)的数据(包含重复的值):
SELECT country, name FROM Websites
WHERE country='CN'
UNION ALL
SELECT country, app_name FROM apps
WHERE country='CN'
ORDER BY country;

JOIN operator

Use MySQL's JOIN to query data in two or more tables.

You can use Mysql JOIN in SELECT, UPDATE and DELETE statements to combine multi-table queries, updates and deletes

JOIN is roughly divided into the following three categories according to its function:

  • INNER JOIN (inner join, or equal join) : Get records with matching fields in two tables.
  • LEFT JOIN (left join): Get all records in the left table, even if there are no matching records in the right table.
  • RIGHT JOIN (right join):  In contrast to LEFT JOIN, it is used to get all records in the right table, even if there are no corresponding matching records in the left table.

Example

There are two tables hanscal_tb and hanke_tb in the database:

INNER JOIN

# INNER JOIN 示例
SELECT a.hanscal_id, a.hanscal_author, b.hanscal_count FROM hanscal_tb a \
INNER JOIN hanke_tb b ON a.hanscal_author = b.hanscal_author; 

# 以上 SQL 语句等价于,带有where子句的INNER_JOIN示例
SELECT a.hanscal_id, a.hanscal_author, b.hanscal_count FROM hanscal_tb a hanke_tb b \ 
WHERE a.hanscal_author = b.hanscal_author; 


LEFT JOIN

 MySQL LEFT JOIN will read all the data in the left data table, even if the right table has no corresponding data.

example

# 以 hanscal_tb 为左表,hanke_tb 为右表:
SELECT a.hanscal_id, a.hanscal_author, b.hanscal_count FROM hanscal a \
LEFT JOIN hanke_tb b ON a.hanscal_author = b.hanscal_author;

In the above example, LEFT JOIN is used. This statement will read all the selected field data of the left data table hanscal_tb, even if there is no corresponding hanscal_author field value in the right table hanke_tb.


RIGHT JOIN

MySQL RIGHT JOIN will read all the data in the data table on the right, even if there is no corresponding data in the table on the left.

example

# 以 hanscal_tb 为左表,hanke_tb 为右表:
SELECT a.hanscal_id, a.hanscal_author, b.hanscal_count FROM hanscal a \
RIGHT JOIN hanke_tb b ON a.hanscal_author = b.hanscal_author;

RIGHT JOIN is used in the above example, this statement will read all the selected field data of the right data table hanke_tb, even if there is no corresponding hanscal_author field value in the left table hanscal_tb.

Guess you like

Origin blog.csdn.net/weixin_43145427/article/details/124143567
Recommended