1.SQL JOIN
NNER JOIN:如果表中有至少一个匹配,则返回行
LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行
RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行
FULL JOIN:只要其中一个表中存在匹配,则返回行
2.SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;
注释:INNER JOIN 与 JOIN 是相同的,取2表共有的。
3.SQL LEFT JOIN 语法
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
注释:在某些数据库中,LEFT JOIN 称为 LEFT OUTER JOIN。
4.SQL RIGHT JOIN 语法
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;
RIGHT JOIN 关键字从右表(Websites)返回所有的行,即使左表(access_log)中没有匹配。
5.RIGHT JOIN 关键字从右表(Websites)返回所有的行,即使左表(access_log)中没有匹配。
SELECT Websites.name, access_log.count, access_log.date
FROM Websites
FULL OUTER JOIN access_log
ON Websites.id=access_log.site_id
ORDER BY access_log.count DESC;
FULL OUTER JOIN 关键字返回左表(Websites)和右表(access_log)中所有的行。