欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

浅谈sql连接查询的区别 inner,left,right,full

程序员文章站 2023-12-17 17:32:04
--table1 表 id name qq phone 1 秦云 10102800 13500000 2 在路上 10378 13600000 3 leo 1000...

--table1 表

id name qq phone
1 秦云 10102800 13500000
2 在路上 10378 13600000
3 leo 10000 13900000
4 秦云 0241458 54564512

--table2 表

id name sjsj gly
1 秦云 2004-01-01 00:00:00.000 李大伟
2 秦云 2005-01-01 00:00:00.000 马化腾
3 在路上 2005-01-01 00:00:00.000 马化腾
4 秦云 2005-01-01 00:00:00.000 李大伟
5 在路上 2005-01-01 00:00:00.000 李大伟

--inner join   只要是符合要求的就显示

select * from table1 t1 

inner join table2 t2 on t1.name=t2.name<br><br>--得到的结果

id name qq phone id name sjsj gly
1 秦云 10102800 13500000 1 秦云 2004-01-01 00:00:00.000 李大伟
4 秦云 0241458 54564512 1 秦云 2004-01-01 00:00:00.000 李大伟
1 秦云 10102800 13500000 2 秦云 2005-01-01 00:00:00.000 马化腾
4 秦云 0241458 54564512 2 秦云 2005-01-01 00:00:00.000 马化腾
2 在路上 10378 13600000 3 在路上 2005-01-01 00:00:00.000 马化腾
1 秦云 10102800 13500000 4 秦云 2005-01-01 00:00:00.000 李大伟
4 秦云 0241458 54564512 4 秦云 2005-01-01 00:00:00.000 李大伟
2 在路上 10378 13600000 5 在路上 2005-01-01 00:00:00.000 李大伟

--left join 以左表为主,会看到若右表有 关联字段重复,则 左边关联的字段信息翻倍。

select * from table1 t1                      (1)

left join table2 t2 on t1.name=t2.name

与

select * from table2 t2                      (2)

right join table1 t1 on t1.name=t2.name

结果相等,不过两表的顺序换一下而已。

 

 

(1)结果

id name  qq phone  id name  sjsj  gly

1  秦云 10102800  13500000  1  秦云 2004-01-01 00:00:00.000 李大伟

1  秦云 10102800  13500000  2  秦云 2005-01-01 00:00:00.000 马化腾

1  秦云 10102800  13500000  4  秦云 2005-01-01 00:00:00.000 李大伟

2  在路上 10378  13600000  3  在路上 2005-01-01 00:00:00.000 马化腾

2  在路上 10378  13600000  5  在路上 2005-01-01 00:00:00.000 李大伟

3  leo 10000  13900000  null  null  null  null

4  秦云 0241458 54564512  1  秦云 2004-01-01 00:00:00.000 李大伟

4  秦云 0241458 54564512  2  秦云 2005-01-01 00:00:00.000 马化腾

4  秦云 0241458 54564512  4  秦云 2005-01-01 00:00:00.000 李大伟

 

(2)结果

id name  sjsj  gly id name  qq phone

1  秦云 2004-01-01 00:00:00.000 李大伟 1  秦云 10102800  13500000

2  秦云 2005-01-01 00:00:00.000 马化腾 1  秦云 10102800  13500000

4  秦云 2005-01-01 00:00:00.000 李大伟 1  秦云 10102800  13500000

3  在路上 2005-01-01 00:00:00.000 马化腾 2  在路上 10378  13600000

5  在路上 2005-01-01 00:00:00.000 李大伟 2  在路上 10378  13600000

null  null  null  null  3  leo 10000  13900000

1  秦云 2004-01-01 00:00:00.000 李大伟 4  秦云 0241458 54564512

2  秦云 2005-01-01 00:00:00.000 马化腾 4  秦云 0241458 54564512

4  秦云 2005-01-01 00:00:00.000 李大伟 4  秦云 0241458 54564512

--left join 已右表为主 ,与left 同理

select * from table1 t1 

right join table2 t2 on t1.name=t2.name

 

id name  qq phone  id name  sjsj  gly

1  秦云 10102800  13500000  1  秦云 2004-01-01 00:00:00.000 李大伟

4  秦云 0241458 54564512  1  秦云 2004-01-01 00:00:00.000 李大伟

1  秦云 10102800  13500000  2  秦云 2005-01-01 00:00:00.000 马化腾

4  秦云 0241458 54564512  2  秦云 2005-01-01 00:00:00.000 马化腾

2  在路上 10378  13600000  3  在路上 2005-01-01 00:00:00.000 马化腾

1  秦云 10102800  13500000  4  秦云 2005-01-01 00:00:00.000 李大伟

4  秦云 0241458 54564512  4  秦云 2005-01-01 00:00:00.000 李大伟

2  在路上 10378  13600000  5  在路上 2005-01-01 00:00:00.000 李大伟 

--full join  两表数据都取出来,不管是否符合

select * from table1 t1 

full join table2 t2 on t1.name=t2.name

 

结果

id name  qq phone  id name  sjsj  gly

1  秦云 10102800  13500000  1  秦云 2004-01-01 00:00:00.000 李大伟

1  秦云 10102800  13500000  2  秦云 2005-01-01 00:00:00.000 马化腾

1  秦云 10102800  13500000  4  秦云 2005-01-01 00:00:00.000 李大伟

2  在路上 10378  13600000  3  在路上 2005-01-01 00:00:00.000 马化腾

2  在路上 10378  13600000  5  在路上 2005-01-01 00:00:00.000 李大伟

3  leo 10000  13900000  null  null  null  null

4  秦云 0241458 54564512  1  秦云 2004-01-01 00:00:00.000 李大伟

4  秦云 0241458 54564512  2  秦云 2005-01-01 00:00:00.000 马化腾

4  秦云 0241458 54564512  4  秦云 2005-01-01 00:00:00.000 李大伟

以上就是小编为大家带来的浅谈sql连接查询的区别 inner,left,right,full全部内容了,希望大家多多支持~

上一篇:

下一篇: