当前位置:   article > 正文

left join 基本用法_left join用法

left join用法

 

废话不多说,来看例子

一、建表,导入测试数据

  1. create table temp1
  2. (
  3. aid VARCHAR2(5) not null,
  4. car VARCHAR2(10) not null
  5. );
  6. create table temp2
  7. (
  8. bid VARCHAR2(5) not null,
  9. username VARCHAR2(10) not null
  10. );
  11. create table temp3
  12. (
  13. cid VARCHAR2(5) not null,
  14. dogname VARCHAR2(10) not null
  15. );
  16. insert into temp1(aid,car) values('001','benz');
  17. insert into temp1(aid,car) values('001','BMW');
  18. insert into temp1(aid,car) values('001','ford');
  19. insert into temp1(aid,car) values('001','jeep');
  20. insert into temp1(aid,car) values('002','jeep');
  21. insert into temp1(aid,car) values('003','hongqi');
  22. insert into temp2(bid,username) values('001','mayun');
  23. insert into temp3(cid,dogname) values('001','lily');
  24. insert into temp3(cid,dogname) values('001','lucy');
  25. insert into temp3(cid,dogname) values('002','xiaohua');

查一下数据长什么样:

  1. select * from temp1;
  2. select * from temp2;
  3. select * from temp3;

 

temp1temp2temp3

 

 

 

 

 

 

二、左连接测试 

--1.左连接,把左边的全部查出来,右边有的则匹配,没有则为null

select * from temp1 t1 left join temp2 t2 on t1.aid=t2.bid ;

 

select * from temp2 t2 left join temp1 t1 on t2.bid=t1.aid ;

--2.若是三张表,通过两个left join来连接,则把前面两张表先left join之后当作一张表,然后再与第三张表left join,同理,多张表的left join 以此类推

select * from temp1 t1 left join temp2 t2 on t1.aid=t2.bid left join temp3 t3 on t2.bid=t3.cid ;

select * from temp3 t3 left join temp1 t1 on t3.cid=t1.aid left join temp2 t2 on t3.cid=t2.bid;

--3.right join 与left join相对应,会将右边的数据全部查出来(例子略)

 

 

-- 一年多以后回过头来,发现第三张表的数据没有造好,也不想更正了,将就看吧,见谅

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AI数据灵魂/article/detail/61873
推荐阅读
相关标签
  

闽ICP备14008679号