LEFT JOIN 关键字从左表(table1)返回全部的行,即便右表(table2)中没有匹配。若是右表中没有匹配,则结果为 NULL。html
语法:sql
SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name=table2.column_name;
举例:ide
下面是选自 "Websites" 表的数据:网站
下面是 "access_log" 网站访问记录表的数据:code
SELECT Websites.name, access_log.count, access_log.date FROM Websites LEFT JOIN access_log ON Websites.id=access_log.site_id ORDER BY access_log.count DESC;
结果:htm
RIGHT JOIN 关键字从右表(table2)返回全部的行,即便左表(table1)中没有匹配。若是左表中没有匹配,则结果为 NULL。blog
语法:get
SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name=table2.column_name;
举例it
SELECT Websites.name, access_log.count, access_log.date FROM access_log RIGHT JOIN Websites ON access_log.site_id=Websites.id ORDER BY access_log.count DESC;