MYSQL 左鏈接與右鏈接

1、 LEFT JOIN

LEFT JOIN 關鍵字從左表(table1)返回全部的行,即便右表(table2)中沒有匹配。若是右表中沒有匹配,則結果爲 NULL。html

語法:sql

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;

MYSQL 左鏈接與右鏈接

舉例:ide

下面是選自 "Websites" 表的數據:網站

MYSQL 左鏈接與右鏈接

下面是 "access_log" 網站訪問記錄表的數據:code

MYSQL 左鏈接與右鏈接

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

MYSQL 左鏈接與右鏈接

2、RIGHT JOIN

RIGHT JOIN 關鍵字從右表(table2)返回全部的行,即便左表(table1)中沒有匹配。若是左表中沒有匹配,則結果爲 NULL。blog

語法:get

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;

MYSQL 左鏈接與右鏈接

舉例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;

原文地址:http://www.runoob.com/sql/sql-join-right.htmltable

相關文章
相關標籤/搜索