join连接

问:查询id是1的学生所在班级的班主任的名字

将两张表变成一张表(除了部分字段命名重复的问题以外,可以将它看成一张表):用join:

eg: select * from mysql_day1 join scores

答上面问题: select stu.id ,stu.name, stu.classid,class.manager from stu join class  on stu.classid = class.id where stu.id = 1

leftjoin

leftjoin与join的区别:

1、left join 必须有on

2、会检查左边表的数据是否都包含在新生成的表中

  如果是,则与join没区别;

  如果不是,则用null与不包含的行组成新行加入新表当中。

如下: select * from stu left join class on stu.classid = class.id --->结果有null出现

  select * from class left join stu on stu.classid = class.id   --> 结果没有Null

right join补充: 

A right join B = B left join A

left join 与 join的区别:

情景一: 有两个班级,查询学生A的班主任是谁。

思路:join俩表后,用where过滤即可

select stu.id,stu.name from stu left join class on stu.classid = class.id where stu.name = 'A'

情景二: 有两个班级,此时删除一个班级信息,查询出目前没有班级的人

select stu.id,stu.name from stu left join class on stu.classid = class.id where class.id is empty

注意:查找null时,需用is

原文地址:https://www.cnblogs.com/fjwjw/p/11543303.html