概念:外连接将查询多个表中相关联的行.
分类:外连接分为左外连接(LEFT JOIN)和右外连接(RIGHT JOIN)
左外连接:显示左表全部记录,右表满足连接条件的记录.
右外连接:显示右表全部记录,左表满足连接条件的记录.
语法:SELECT column_list
FROM t1
LEFT|RIGHT JOIN t2
ON join_condition1
案例:
需求:根据业务需要,我们需要查看图书类别表中的所有类别下都有哪些图书.
SQL语句:(左外连接)
select book_id, book_name, category
FROM bookcategory
LEFT JOIN bookinfo
ON bookcategory.category_id = bookinfo.book_category_id
WHERE parent_id<>0;
SQL语句:(右外连接)
select book_id, book_name, category
from bookinfo a
right join bookcategory b
on b.category_id = a.book_category_id;
WHERE parent_id<>0;