数据库,inner join,left join right join 的区别

假设有两个表:

学生和课程

student:              class:

id    student          id       class    studentId

1         张三           1         语文           2

2         小红           2         数学    2

4         小王           3         英语    3

5         刘兰           4         化学    3

内连接:

select s.student, c.class from student s inner join class c on s.id=c.studentId

这样就会把符合这两个条件的内容查出来

student      class

  小红     语文

  小红           数学

当然我们一般会用下面的常用简写:

select s.student, c.class from student s,class c where s.id=c.studentId

而left join则是会将左边的表全部查找出来,即使他没有报名任何课程

select s.student, c.class from student s left join class c on s.id=c.studentId

student       class

  

    张三          null

    小红     语文

    小红           数学        

     小王         null    

    刘兰          null

想对的right join 则是将右边表的内容全部查找一遍,那么结果就变为

student       class

    小红     语文

    小红           数学        

    null          英语  

    null         化学    

原文地址:https://www.cnblogs.com/weblv/p/5210822.html