SQL 三表联查

三表联查

  • 创建三张表
  • 来三表联查一下吧
    • inner join 内连接
    • full join 全连接
    • left join 左连接


因需要在三个表中查东西,还不能要交集,要并集。

创建三张表

我用的是DBaver来创建,下面就是代码,跑完之后会出现三个表。

create table tas_master.hk_test_1(
	id INT not null,
	name1 varchar(100) NULL
);

create table tas_master.hk_test_2(
	id INT not null,
	name1 varchar(100) null,
	name2 varchar(100) NULL
);

create table tas_master.hk_test_3(
	id INT not null,
	name1 varchar(100) null,
	name3 varchar(100) NULL
);


insert into tas_master.hk_test_1 
(id, name1) 
values 
(1, 's1'),
(2, 's2'),
(3, 's3');


insert into tas_master.hk_test_2 
(id, name1, name2) 
values 
(2, 's2', '2s2'),
(3, 's3', '3s3'),
(4, 's4', '4s4');


insert into tas_master.hk_test_3 
(id, name1, name3) 
values 
(1, 's1', '1l1'),
(1, 'ss1', '1l1'),
(2, 's2', '2l2'),
(3, 's3', '3l3'),
(4, 's4', '4l4'),
(5, 's5', '5l5');

select * from tas_master.hk_test_1;
select * from tas_master.hk_test_2;
select * from tas_master.hk_test_3;

第一个表tas_master.hk_test_1

id name1
2 s2
3 s3
1 s1

第二个表tas_master.hk_test_2

id name1 name2
2 s2 2s2
3 s3 3s3
4 s4 4s4

第三个表tas_master.hk_test_3

id name1 name3
3 s3 3l3
2 s2 2l2
5 s5 5l5
1 s1 1l1
1 ss1 1l1
4 s4 4l4

来三表联查一下吧

就是选用三个表的数据,一定是满足on条件的。

inner join 内连接

select coalesce(t1.id, t2.id, t3.id) as id, t1.name1, t2.name1, t2.name2, t3.name1, t3.name3 from 
(select * from tas_master.hk_test_1) as t1 
join (select * from tas_master.hk_test_2) as t2 
on (t1.id = t2.id and t1.name1 = t2.name1) 
join (select * from tas_master.hk_test_3) as t3
on (t2.id = t3.id and t3.name1 = t2.name1);

结果,可看作交集

id name1 name1 name2 name1 name3
3 s3 s3 3s3 s3 3l3
2 s2 s2 2s2 s2 2l2

full join 全连接

select coalesce(t1.id, t2.id, t3.id) as id, t1.name1, t2.name1, t2.name2, t3.name1, t3.name3 from 
(select * from tas_master.hk_test_1) as t1 
full join (select * from tas_master.hk_test_2) as t2 
on (t1.id = t2.id) 
full join (select * from tas_master.hk_test_3) as t3
on (t1.id = t3.id);

结果,可看作并集,注意on后面只能跟一个条件,多了不好用

id name1 name1 name2 name1 name3
2 s2 s2 2s2 s2 2l2
1 s1 ss1 1l1
1 s1 s1 1l1
5 s5 5l5
4 s4 4s4
4 s4 4l4
3 s3 s3 3s3 s3 3l3

我们看下用不同的on后面条件,出来结果是否一样。

insert into tas_master.hk_test_3 
(id, name1, name3) 
values 
(6, 's1', '1l2');

tas_master.hk_test_3 就变成这样,多了id_6

id name1 name3
3 s3 3l3
6 s1 1l2
5 s5 5l5
2 s2 2l2
1 s1 1l1
1 ss1 1l1
4 s4 4l4

name1条件

select coalesce(t1.id, t2.id, t3.id) as id, t1.name1, t2.name1, t2.name2, t3.name1, t3.name3 from 
(select * from tas_master.hk_test_1) as t1 
full join (select * from tas_master.hk_test_2) as t2 
on (t1.name1 = t2.name1) 
full join (select * from tas_master.hk_test_3) as t3
on (t1.name1 = t3.name1);
id name1 name1 name2 name1 name3
3 s3 s3 3s3 s3 3l3
1 ss1 1l1
1 s1 s1 1l2
1 s1 s1 1l1
5 s5 5l5
4 s4 4s4
4 s4 4l4
2 s2 s2 2s2 s2 2l2

id条件

select coalesce(t1.id, t2.id, t3.id) as id, t1.name1, t2.name1, t2.name2, t3.name1, t3.name3 from 
(select * from tas_master.hk_test_1) as t1 
full join (select * from tas_master.hk_test_2) as t2 
on (t1.id = t2.id) 
full join (select * from tas_master.hk_test_3) as t3
on (t1.id = t3.id);
id name1 name1 name2 name1 name3
6 s1 1l2
4 s4 4s4
4 s4 4l4
3 s3 s3 3s3 s3 3l3
2 s2 s2 2s2 s2 2l2
5 s5 5l5
1 s1 ss1 1l1
1 s1 s1 1l1

两个还不是一样的,我认为只要on条件中,筛出来的那一列肯定是有值的。
我有尝试了不同的条件组合,发现这个on条件中还是只放一个为好,下面是我做的记录。

select t1.id, t2.id, t3.id, t1.name1, t2.name1, t2.name2, t3.name1, t3.name3 from 
(select * from tas_master.hk_test_1) as t1 
full join (select * from tas_master.hk_test_2) as t2 
on (t1.id = t2.id) 
full join (select * from tas_master.hk_test_3) as t3
on (t2.id = t3.id and t1.id = t3.id and t1.id = t2.id);
id id id name1 name1 name2 name1 name3
3 3 3 s3 s3 3s3 s3 3l3
6 s1 1l2
1 s1
1 ss1 1l1
1 s1 1l1
2 2 2 s2 s2 2s2 s2 2l2
4 s4 4s4
4 s4 4l4
5 s5 5l5
select t1.id, t2.id, t3.id, t1.name1, t2.name1, t2.name2, t3.name1, t3.name3 from 
(select * from tas_master.hk_test_1) as t1 
full join (select * from tas_master.hk_test_2) as t2 
on (t1.id = t2.id) 
full join (select * from tas_master.hk_test_3) as t3
on (t2.id = t3.id and t1.id = t3.id);
id id id name1 name1 name2 name1 name3
3 3 3 s3 s3 3s3 s3 3l3
1 s1
1 ss1 1l1
1 s1 1l1
6 s1 1l2
5 s5 5l5
2 2 2 s2 s2 2s2 s2 2l2
4 s4 4s4
4 s4 4l4
select t1.id, t2.id, t3.id, t1.name1, t2.name1, t2.name2, t3.name1, t3.name3 from 
(select * from tas_master.hk_test_1) as t1 
full join (select * from tas_master.hk_test_2) as t2 
on (t1.id = t2.id) 
full join (select * from tas_master.hk_test_3) as t3
on (t2.id = t3.id);
id id id name1 name1 name2 name1 name3
3 3 3 s3 s3 3s3 s3 3l3
5 s5 5l5
4 4 s4 4s4 s4 4l4
2 2 2 s2 s2 2s2 s2 2l2
1 s1
1 ss1 1l1
1 s1 1l1
6 s1 1l2

left join 左连接

这个就简单了就是以左边的为准。

select t1.id, t2.id, t3.id, t1.name1, t2.name1, t2.name2, t3.name1, t3.name3 from 
(select * from tas_master.hk_test_1) as t1 
left join (select * from tas_master.hk_test_2) as t2 
on (t1.id = t2.id) 
left join (select * from tas_master.hk_test_3) as t3
on (t1.id = t3.id);
id id id name1 name1 name2 name1 name3
1 1 s1 ss1 1l1
1 1 s1 s1 1l1
3 3 3 s3 s3 3s3 s3 3l3
2 2 2 s2 s2 2s2 s2 2l2
select t1.id, t2.id, t3.id, t1.name1, t2.name1, t2.name2, t3.name1, t3.name3 from 
(select * from tas_master.hk_test_1) as t1 
left join (select * from tas_master.hk_test_2) as t2 
on (t1.id = t2.id) 
left join (select * from tas_master.hk_test_3) as t3
on (t3.id = t2.id);
id id id name1 name1 name2 name1 name3
1 s1
3 3 3 s3 s3 3s3 s3 3l3
2 2 2 s2 s2 2s2 s2 2l2

给我的感觉是,三表联查,主要是看on条件中是什么样的。想三表联查还是with写好点。

你可能感兴趣的:(#,《数据库系统》,sql,java,前端)