MySQL 关联查询 外连接 { LEFT| RIGHT } JOIN

左外连接: (以左表为基准)
两张表做连接的时候,在连接条件不匹配的时候
留下左表中的数据,而右表中的数据以NULL填充
例:使用左连接把学生的数据全取出来,该学生没有学院信息的用NULL填充

mysql>  select * from  student  left join  department
         ->  on dept_id= d_id;


右外连接: (以右表为基准)
对两张表做连接的时候,在连接条件不匹配的时候
留下右表中的数据,而左表中的数据以NULL填充
例:使用右外连接,把没有的学院的数据也显示出来
insert into department(d_name)  value('艺术学院’);

mysql>   select * from  student right join department
         ->   on dept_id= d_id;

猜你喜欢

转载自www.cnblogs.com/mariobear/p/9180542.html