--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 NAMEQQ PHONEID NAMEsjsjgly1秦云 10102800135000001秦云 2004-01-01 00:00:00.000 李大伟1秦云 10102800135000002秦云 2005-01-01 00:00:00.000 马化腾1秦云 10102800135000004秦云 2005-01-01 00:00:00.000 李大伟2在路上 10378136000003在路上 2005-01-01 00:00:00.000 马化腾2在路上 10378136000005在路上 2005-01-01 00:00:00.000 李大伟3LEO 1000013900000NULLNULLNULLNULL4秦云 0241458 545645121秦云 2004-01-01 00:00:00.000 李大伟4秦云 0241458 545645122秦云 2005-01-01 00:00:00.000 马化腾4秦云 0241458 545645124秦云 2005-01-01 00:00:00.000 李大伟 (2)结果ID NAMEsjsjgly ID NAMEQQ PHONE1秦云 2004-01-01 00:00:00.000 李大伟 1秦云 10102800135000002秦云 2005-01-01 00:00:00.000 马化腾 1秦云 10102800135000004秦云 2005-01-01 00:00:00.000 李大伟 1秦云 10102800135000003在路上 2005-01-01 00:00:00.000 马化腾 2在路上 10378136000005在路上 2005-01-01 00:00:00.000 李大伟 2在路上 1037813600000NULLNULLNULLNULL3LEO 10000139000001秦云 2004-01-01 00:00:00.000 李大伟 4秦云 0241458 545645122秦云 2005-01-01 00:00:00.000 马化腾 4秦云 0241458 545645124秦云 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 NAMEQQ PHONEID NAMEsjsjgly1秦云 10102800135000001秦云 2004-01-01 00:00:00.000 李大伟4秦云 0241458 545645121秦云 2004-01-01 00:00:00.000 李大伟1秦云 10102800135000002秦云 2005-01-01 00:00:00.000 马化腾4秦云 0241458 545645122秦云 2005-01-01 00:00:00.000 马化腾2在路上 10378136000003在路上 2005-01-01 00:00:00.000 马化腾1秦云 10102800135000004秦云 2005-01-01 00:00:00.000 李大伟4秦云 0241458 545645124秦云 2005-01-01 00:00:00.000 李大伟2在路上 10378136000005在路上 2005-01-01 00:00:00.000 李大伟
--full join 两表数据都取出来,不管是否符合select * from table1 t1 full join table2 t2 on t1.name=t2.name 结果ID NAMEQQ PHONEID NAMEsjsjgly1秦云 10102800135000001秦云 2004-01-01 00:00:00.000 李大伟1秦云 10102800135000002秦云 2005-01-01 00:00:00.000 马化腾1秦云 10102800135000004秦云 2005-01-01 00:00:00.000 李大伟2在路上 10378136000003在路上 2005-01-01 00:00:00.000 马化腾2在路上 10378136000005在路上 2005-01-01 00:00:00.000 李大伟3LEO 1000013900000NULLNULLNULLNULL4秦云 0241458 545645121秦云 2004-01-01 00:00:00.000 李大伟4秦云 0241458 545645122秦云 2005-01-01 00:00:00.000 马化腾4秦云 0241458 545645124秦云 2005-01-01 00:00:00.000 李大伟
以上就是小编为大家带来的浅谈sql连接查询的区别 inner,left,right,full全部内容了,希望大家多多支持脚本之家~