您好,登錄后才能下訂單哦!
本篇內容介紹了“MySQL多表查詢案例分析”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
笛卡爾積的理解
select id,department_name from employees,departments;#錯的 select id,department_id,department_name from employees CROSS JOIN departments;#錯的
每個員工和每個部門匹配了一遍(查出的條目數=id數*department數)
錯誤原因:缺少連接條件
編寫連接條件: 表1.列 = 表二.列(若多個表連接,至少要用n-1個連接條件)
select id,employees.name,department_name from employees,departments WHERE employees.name = departments.name;
注:如果要顯示的列在要查詢的表中名字一樣,則要表明,是出自哪個表, eg: employees.name
建議在多表查詢時,標明顯示的是哪個表的信息 (優化)
優化:可以在FROM后使用表的別名,但是一旦使用別名,后續就一定要都用別名
等值連接:上述的帶有=的
非等值連接:沒有=的
select t1.id,t1.name,t2.grade from employees t1,departments t2 WHERE ti.salary BETWEEN t2.lowest_salary AND t2.highest_salary ;#非等值
非自鏈接:表1和表2連接
自鏈接:表1和自己連接
#顯示員工(t1)和其管理者(t2)的基本信息 select t1.id,t1.name,t2.id,t2.name from employees t1,employees t2#一個表看作兩個表 WHERE t1.manage_id = t2.id ;#自連接
內連接:合并含有同一列的表,結果不包括一個表與另一個表不匹配打的行
外連接:合并含有同一列的表,結果除了內連接的結果還查詢不匹配的行
外連接的分類:左外連接(左表多,補右邊),右外連接(右表多,補左邊),滿外連接
內連接:見上
外連接:左表有數據不匹配,在右表加(+);反之,在左表加(+),但是MySQL不支持
WHERE t1.department_id = t2.department_id(+)#左連接
select t1.id,t1.name,t2.department_name,t3.environment from employees t1 JOIN departments t2 ON t1.department_id = t2.department_id JOIN locations t3#加入第二個人表 ON t2.department_location = t3.department_location;
使用OUTER JOIN...ON...
左外連接:LEFT OUTER JOIN
右外連接:RIGHT OUTER JOIN
滿外連接:FULL OUTER JOIN(MySQL不支持)
select t1.name,t2.department_name#左外連接 from employees t1 LEFT OUTER(可省略) JOIN departments t2 ON t1.department_id = t2.department_id;
合并查詢結果
SELECT colum... FROM table1 UNION (ALL) SELECT colum... FROM table2
UNION操作符
兩個查詢結果的并集,去重(效率低)
UNION ALL操作符(推薦)
兩個查詢結果的并集,不去重(效率高)
7種SQL JOINS的實現
中圖(內連接):
select t1.name,t2.department_name from employees t1 JOIN departments t2 ON t1.department_id = t2.department_id;
左上圖(左外連接):
select t1.name,t2.department_name from employees t1 LEFT JOIN departments t2 ON t1.department_id = t2.department_id;
右上圖(右外連接):
select t1.name,t2.department_name from employees t1 RIGHT JOIN departments t2 ON t1.department_id = t2.department_id;
左中圖:
select t1.name,t2.department_name from employees t1 LEFT JOIN departments t2 ON t1.department_id = t2.department_id WHERE t2.department_id IS NULL;
右中圖:
select t1.name,t2.department_name from employees t1 RIGHT JOIN departments t2 ON t1.department_id = t2.department_id WHERE t1.department_id IS NULL;
左下圖(滿外連接):
#方式一:左上圖 UNION ALL 右中圖 select t1.name,t2.department_name from employees t1 LEFT JOIN departments t2 ON t1.department_id = t2.department_id UNION ALL select t1.name,t2.department_name from employees t1 RIGHT JOIN departments t2 ON t1.department_id = t2.department_id WHERE t1.department_id IS NULL; #方式二:左中圖 UNION ALL 右上圖 select t1.name,t2.department_name from employees t1 LEFT JOIN departments t2 ON t1.department_id = t2.department_id WHERE t2.department_id IS NULL UNION ALL select t1.name,t2.department_name from employees t1 RIGHT JOIN departments t2 ON t1.department_id = t2.department_id;
右下圖:
#左中圖 UNION ALL 右中圖 select t1.name,t2.department_name from employees t1 LEFT JOIN departments t2 ON t1.department_id = t2.department_id WHERE t2.department_id IS NULL UNION ALL select t1.name,t2.department_name from employees t1 RIGHT JOIN departments t2 ON t1.department_id = t2.department_id WHERE t1.department_id IS NULL;
使用關鍵字:NATURAL JOIN(不靈活),自動查詢表中所有相同字段,然后進行等值連接
使用關鍵字:USING(同名字段),將表中相同名字的字段自動等值連接
select t1.name,t2.department_name from employees t1 JOIN departments t2 ON t1.department_id = t2.department_id; 等價于 select t1.name,t2.department_name from employees t1 JOIN departments t2 USING(department_id);
“MySQL多表查詢案例分析”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。