Join从句

Join从句

引用两个数据库举例说明:

Join从句

 

Join从句

 

1.内连接

select a.name,a.do,b.name,b.do
from person a INNER JOIN friend b
on a.name = b.name;

Join从句

 

2.全连接

select a.name,a.do,b.name,b.do
from person a LEFT JOIN friend b
on a.name = b.name
UNION ALL
select a.name,a.do,b.name,b.do
from person a RIGHT JOIN friend b
on a.name = b.name;

Join从句

 

3.左连接

select a.name,a.do,b.name,b.do
from person a LEFT JOIN friend b
on a.name = b.name;

Join从句

 4.右连接

select a.name,a.do,b.name,b.do
from person a RIGHT JOIN friend b
on a.name = b.name;

Join从句

 5.交叉连接

select a.name,a.do,b.name,b.do
from person a CROSS JOIN  friend b;

Join从句

 下面是很有意思的两个使用技巧:

1.更新表

UPDATE person a JOIN(
select b.name
from person a INNER JOIN friend b
on a.name=b.name
) b on a.name = b.name
 set a.do = "test";

Join从句

 2.优化子查询

优化前:
select a.name,a.do,(
select do from friend b
where a.name = b.name
) as do2
from person a;

优化后:
select a.name,a.do,b.do as do2
from person a LEFT JOIN friend b
on a.name = b.name;

Join从句

今天是2021年11月7日,立冬,济南迎来了2021年的第一场大雪,开心

上一篇:linux文件目录管理命令


下一篇:【IOI2014】Friend 题解