SQL语法(UNION,JOIN)

SQL语法

union, union all

UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
注意,UNION 内部的每个SELECT语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每个SELECT语句中的列的顺序必须相同。

UNION语法

SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
注释:默认地,UNION 操作符选取不同的值。如果允许重复的值,请使用 UNION ALL。

UNION ALL语法

SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
注释:UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名。

JOIN

举例我们有两张表,t1和t2。

drop database if exists db1;
create database if not exists db1 character set utf8mb4;
use db1;

drop table if exists t1;
create table if not exists t1(
    `id` int not null primary key,
    `name` varchar(10) not null);
insert into t1(`id`,`name`)
    values(1,"Google"),(2,"Alibaba"),(3,"Oracle"),(4,"PingCAP");

drop table if exists t2;
create table if not exists t2(
    `id` int not null primary key,
    `address` varchar(10) not null);
insert into t2(`id`,`address`)
    values(1,"USA"),(2,"CN"),(5,"UK"),(4,"CN");

select * from t1 order by `id`;
select * from t2 order by `id`;
+----+---------+
| id | name    |
+----+---------+
|  1 | Google  |
|  2 | Alibaba |
|  3 | Oracle  |
|  4 | PingCAP |
+----+---------+

+----+---------+
| id | address |
+----+---------+
|  1 | USA     |
|  2 | CN      |
|  4 | CN      |
|  5 | UK      |
+----+---------+

JOIN(INNER JOIN)

内连接是最常见的一种连接,只连接匹配的行。
JOIN语法

SELECT column_name(s) FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name

执行SQL语句

select * from t1 join t2 on t1.id = t2.id order by t1.id;

输出结果如下

+----+---------+----+---------+
| id | name    | id | address |
+----+---------+----+---------+
|  1 | Google  |  1 | USA     |
|  2 | Alibaba |  2 | CN      |
|  4 | PingCAP |  4 | CN      |
+----+---------+----+---------+

LEFT JOIN(LEFT OUTER JOIN)

LEFT JOIN返回左表的全部行和右表满足ON条件的行,如果左表的行在右表中没有匹配,那么这一行右表中对应数据用NULL代替。
LEFT JOIN 语法

SELECT column_name(s) FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name

LEFT JOIN产生表1的完全集,而2表中匹配的则有值,没有匹配的则以null值取代。
执行SQL语句

select * from t1 left join t2 on t1.id = t2.id order by t1.id;

输出结果如下

+----+---------+------+---------+
| id | name    | id   | address |
+----+---------+------+---------+
|  1 | Google  |    1 | USA     |
|  2 | Alibaba |    2 | CN      |
|  3 | Oracle  | NULL | NULL    |
|  4 | PingCAP |    4 | CN      |
+----+---------+------+---------+

RIGHT JOIN(RIGHT OUTER JOIN)

RIGHT JOIN返回右表的全部行和左表满足ON条件的行,如果右表的行在左表中没有匹配,那么这一行左表中对应数据用NULL代替。
RIGHT JOIN语法

SELECT column_name(s) FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name

RIGHT JOIN产生表2的完全集,而1表中匹配的则有值,没有匹配的则以null值取代。
执行SQL语句

select * from t1 right join t2 on t1.id = t2.id order by t2.id;

输出结果如下

+------+---------+----+---------+
| id   | name    | id | address |
+------+---------+----+---------+
|    1 | Google  |  1 | USA     |
|    2 | Alibaba |  2 | CN      |
|    4 | PingCAP |  4 | CN      |
| NULL | NULL    |  5 | UK      |
+------+---------+----+---------+

FULL OUTER JOIN

FULL JOIN 会从左表和右表那里返回所有的行。如果其中一个表的数据行在另一个表中没有匹配的行,那么对面的数据用NULL代替。
FULL OUTER JOIN语法

SELECT column_name(s) FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name

FULL OUTER JOIN产生1和2的并集。但是需要注意的是,对于没有匹配的记录,则会以null做为值。
执行SQL语句

select * from t1 full outer join t2 on t1.id = t2.id order by t1.id;
原文地址:https://www.cnblogs.com/jie828/p/11422815.html