时间:2021-07-01 10:21:17 帮助过:4人阅读
(1)左连接:根据左表的记录,在被连接的右表中找出符合条件的记录与之匹配,如果找不到与左表匹配的,用null表示 [x left [outer] join y on...]
第一种写法:left join .. on .. SELECT t.TEACHER_NAME,s.STUDENT_NAME FROM teacher t LEFT JOIN student s ON t.ID=s.TEACHER_ID
第二种写法:left outer join .. on .. SELECT t.TEACHER_NAME,s.STUDENT_NAME FROM teacher t LEFT OUTER JOIN student s ON t.ID=s.TEACHER_ID
(2)右连接:根据右表的记录,在被连接的左表中找出符合条件的记录与之匹配,如果找不到匹配的,用null填充 [x right [outer] join y on...]
第一种写法:right join .. on .. SELECT t.TEACHER_NAME,s.STUDENT_NAME FROM teacher t RIGHT JOIN student s ON t.ID=s.TEACHER_ID
第二种写法:right outer join .. on .. SELECT t.TEACHER_NAME,s.STUDENT_NAME FROM teacher t RIGHT OUTER JOIN student s ON t.ID=s.TEACHER_ID
(3)全连接:返回符合条件的所有表的记录,没有与之匹配的,用null表示(结果是左连接和右连接的并集)
第一种写法:(full join .. on ..) select t.teacher_name, s.student_name from teacher t full join student s on t.id = s.teacher_id;
第二种写法:(full outer join .. on) select t.teacher_name, s.student_name from teacher t full outer join student s on t.id = s.teacher_id;
注意:Oracle数据库支持full join,mysql是不支持full join的
如果直接查询2张表,将得到一个笛卡尔积,两个表的乘积 select * from teacher,student;
关于笛卡尔积的说明,请参考博客:https://blog.csdn.net/csdn_hklm/article/details/78394412
SQL查询---内连接、外连接、自连接查询
标签:使用 -- com 条件 一个 sql div outer style