-- ----------------
SELECT * FROM websites;
SELECT * FROM access_log;
-- INNER JOIN 从多个表中返回满足 JOIN 条件的所有行
SELECT Websites.id, Websites.name, access_log.count, access_log.date
FROM Websites
INNER JOIN access_log
ON Websites.id=access_log.site_id;
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;
-- RIGHT JOIN 从右表(Websites)返回所有的行,即使左表(table1)中没有匹配。如果左表中没有匹配,则结果为 NULL。
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;
-- MySQL中不支持 FULL OUTER JOIN
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;
SELECT country FROM Websites UNION
SELECT country FROM apps
ORDER BY country;
-- 从 "Websites" 和 "apps" 表中选取所有的country(也有重复的值)
SELECT country FROM Websites
UNION ALL
SELECT country FROM apps
ORDER BY country;
-- 从 "Websites" 和 "apps" 表中选取所有的中国(CN)的数据(也有重复的值):
SELECT country, `name` FROM Websites WHERE country='CN'
UNION ALL
SELECT country, app_name FROM apps WHERE country='CN'
ORDER BY country;