時光在不經(jīng)意間,總是過得出奇的快。小暑已過,進(jìn)入中暑,太陽更加熱烈的綻放著ta的光芒,...在外面被太陽照顧的人們啊,你們都是勤勞與可愛的人啊。在房子里已各種姿勢看我這篇這章的你,既然點了進(jìn)來,那就由我繼續(xù)帶你回顧MySql的知識吧!
回顧練習(xí)資料girls庫以及兩張表的腳本:
鏈接: https://pan.baidu.com/s/1bgFrP7dBBwk3Ao755pU4Qg 提取碼: ihg7
引題:笛卡爾現(xiàn)象,先來觀看一下兩張表。
SELECT NAME,boyname FROM boys,beauty;
最終結(jié)果:12*4=48行
#進(jìn)階6:連接查詢
含義:又稱多表查詢,當(dāng)查詢的字段來自于多個表時,就會用到連接查詢
笛卡兒積現(xiàn)象: 表1 有m行,表2有n行,結(jié)果=m*n行
產(chǎn)生原因:沒有有效的連接條件
解決方法:添加有效的連接條件
連接分類:
按年代分類:
SQL1992標(biāo)準(zhǔn)(192標(biāo)準(zhǔn)):僅支持內(nèi)連接
SQL1999標(biāo)準(zhǔn)(199標(biāo)準(zhǔn)) [推薦]:支持內(nèi)連接+外聯(lián)結(jié)(左外與右外)+交叉連接
按功能分類:
內(nèi)連接:
等值連接
非等值連接
自聯(lián)結(jié)
外連接:
左外連接
右外連接
全外連接
交叉連接:
左外連接
右外連接
全外連接
交叉連接:
SELECT NAME,boyname FROM boys,beauty
WHERE beauty.boyfriend_id = boys.id;
#一.SQL192標(biāo)準(zhǔn)
#1.等值連接
①多表等值連接的結(jié)果為多表的交集部分
②n表連接,至少需要n-1個連接條件
③多表的順序沒有要求
④一般需要為表起別名
⑤可以搭配前面介紹的所有子句使用,比如,排序,分組,篩選。
#多表查詢,先匹配在篩選
#案例1.查詢員工名和對應(yīng)的部門名。
SELECT first_name AS 名,department_name AS 部門名
FROM employees,departments
WHERE employees.department_id = departments.department_id;
#案例2.查詢員工名,工種號,工種名。對于兩張表共有的字段需要加表名作限定不然會報錯。
錯誤示例:
SELECT first_name AS 名,employees.job_id AS 工種號,job_title AS 工種名
FROM employees,jobs
WHERE employees.job_id = jobs.job_id;
#2.為表起別名
①提高語句的簡潔度
②區(qū)分多個重命名的字段
注意:如果為表起了別名,則查詢的字段就不能使用原來的表名去限定
SELECT first_name AS 名,e.job_id AS 工種號,job_title AS 工種名
FROM employees AS e,jobs AS j
WHERE e.job_id = j.job_id;
給表起了別名,再用表完整名子做限定會報錯,不允許。根據(jù)執(zhí)行順序走先走FROM,
走完FROM后就用別名的,相當(dāng)于生成了一個虛擬的視圖,不再認(rèn)原來的表名。
#3.兩個表名的順序是否可以調(diào)換,是可以調(diào)換的。
SELECT first_name AS 名,e.job_id AS 工種號,job_title AS 工種名
FROM jobs AS j,employees AS e
WHERE e.job_id = j.job_id;
#4.可以加篩選
#案例3.查詢有獎金的員工名,部門名。
SELECT first_name AS 名,department_name AS 部門名,commission_pct AS 獎金
FROM employees AS e,departments AS d
WHERE e.department_id=d.department_id
AND commission_pct IS NOT NULL;#AND e.commission_pct IS NOT NULL;
#案例4.查詢城市名中第二個字符為o對應(yīng)的城市名與部門名。
SELECT city AS 城市,department_name AS 部門名
FROM locations AS l,departments AS d
WHERE l.location_id = d.location_id
AND city LIKE '_o%';
#5.可以加分組
#案例1.查詢每個城市的部門個數(shù)。
SELECT city AS 城市,COUNT(department_id) AS 個數(shù)
FROM locations AS l,departments AS d
WHERE l.location_id = d.location_id
GROUP BY l.city;
#案例2.查詢有獎金的每個部門的部門名和部門的領(lǐng)導(dǎo)編號和該部門的最低工資。
#查詢的時候不確定把兩個列都加上。
SELECT commission_pct AS 獎金,department_name AS 部門名,
d.manager_id AS 領(lǐng)導(dǎo)編號,MIN(salary) AS 最低工資
FROM employees AS e,departments AS d
WHERE e.department_id = d.department_id
AND commission_pct IS NOT NULL
GROUP BY department_name,d.manager_id;
#6.可以加排序
#案例1:查詢每個工種的工種名和員工的個數(shù),并且按員工個數(shù)降序。
SELECT j.job_title AS 工種名,COUNT(employee_id) AS 個數(shù)
FROM employees AS e,jobs AS j
WHERE e.job_id = j.job_id
GROUP BY job_title
ORDER BY 個數(shù) DESC;
#7.三表連接
#案例1.查詢員工名,部門名與所在的城市
SELECT first_name AS 名,d.manager_id AS 部門名,city AS 城市
FROM employees AS e,departments AS d,locations AS l
WHERE e.department_id = d.department_id
AND d.location_id = l.location_id;
#案例2.查詢員工名,部門名與所在的城市,城市以s開頭。
SELECT first_name AS 名,d.manager_id AS 部門名,city AS 城市
FROM employees AS e,departments AS d,locations AS l
WHERE e.department_id = d.department_id
AND d.location_id = l.location_id
AND city LIKE 's%';
#案例3.查詢員工名,部門名與所在的城市,城市以s開頭,按姓名降序排列。
SELECT first_name AS 名,d.manager_id AS 部門名,city AS 城市
FROM employees AS e,departments AS d,locations AS l
WHERE e.department_id = d.department_id
AND d.location_id = l.location_id
AND city LIKE 's%'
ORDER BY department_name DESC;
新增一張工資等級表。
CREATE TABLE job_grades
(grade_level VARCHAR(3),
lowest_sal int,
highest_sal int);
INSERT INTO job_grades
VALUES ('A', 1000, 2999);
INSERT INTO job_grades
VALUES ('B', 3000, 5999);
INSERT INTO job_grades
VALUES('C', 6000, 9999);
INSERT INTO job_grades
VALUES('D', 10000, 14999);
INSERT INTO job_grades
VALUES('E', 15000, 24999);
INSERT INTO job_grades
VALUES('F', 25000, 40000);
#2.非等值連接,(范圍判斷)
#案例1.查詢員工的工資和工資級別。
SELECT salary AS 工資,grade_level AS 等級
FROM employees AS e,job_grades AS g
WHERE salary BETWEEN g.lowest_sal AND g.highest_sal;
#案例2.查詢員工的工資和工資級別,展示出A級別的員工。
SELECT salary AS 工資,grade_level AS 等級
FROM employees AS e,job_grades AS g
WHERE salary BETWEEN g.lowest_sal AND g.highest_sal
AND g.grade_level = 'A';
#案例1.查詢員工名和上級的名稱.
SELECT e.employee_id,e.last_name AS 員工,
m.employee_id,m.last_name AS 領(lǐng)導(dǎo)
FROM employees e,employees m
WHERE e.manager_id = m.employee_id;
經(jīng)過了以上的示例,相信你對多表查詢已經(jīng)有了一個了解,趕快動動你的小手手來練習(xí)一下吧!o(^▽^)o
到此這篇關(guān)于MySQL多表查詢詳解上的文章就介紹到這了,更多相關(guān)MySQL多表查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- MySQL多表查詢的具體實例
- MySQL數(shù)據(jù)庫高級查詢和多表查詢
- MySQL多表查詢詳解下
- 淺談MySQL使用笛卡爾積原理進(jìn)行多表查詢
- MySQL 四種連接和多表查詢詳解