MySQL高階6 連線查詢 sql92標準

2021-10-06 12:17:31 字數 4043 閱讀 9574

# 高階6:連線查詢

/*含義:又稱多表查詢,當我們查詢的字段來自於多個表時,就會用到

笛卡爾乘積現象: 表1有m行,表2有n行,結果為m*n行

發生原因,沒有有效的連線條件

如何避免,新增有效的連線條件

分類: 按年代分類:

sql92標準:僅僅支援內連線

按功能分類:

內連線:

等值連線

非等值連線

自連線外連線:

左外連線

右外連線

全外連線

交叉連線

*/use girls;

select

*from beauty;

select

*from boys;

select name,boyname from beauty,boys

# 如上所示,會出現笛卡爾乘積現象

where beauty.boyfriend_id=boys.id;

# 一、sql92標準

# 1.等值連線

/*①多表等值連線的結果為多表的交集部分

②n表連線,至少需要n-1個連線條件

③多表的順序沒有要求

④一般需要為表取別名(提高可讀性)

⑤可以搭配前面介紹的所有子句使用,比如排序,分組,篩選

*/# 案例1:查詢女神名和對應的男生名

select name,boyname from beauty,boys

where beauty.boyfriend_id=boys.id;

# 案例2:查詢員工名和對應的部門名

use myemployees;

select last_name,department_name

from employees,departments

where employees.

`department_id`

=departments.

`department_id`

;# 案例3:查詢員工名、工種號、工種名

select last_name,employees.job_id,job_title

from employees,jobs # 如果經常限定,故要給表名取別名

where employees.

`job_id`

=jobs.

`job_id`;/*

為表起別名(from後)讀入表的順序不影響

①高語句的簡潔度

②區分多個重名的字段

注意,如果為表起了別名,則查詢的字段就不能使用原來的表名去限定

*/# 案例4:查詢有獎金的員工名、部門名

# 知識點:可以加篩選

select last_name,department_name,commission_pct

from employees as e,departments as d

where e.

`department_id`

=d.`department_id`

and e.

`commission_pct`

isnot

null

;# 案例5:查詢城市名中第二個字元為o的部門名和城市名

select department_name,city

from departments as d,locations as l

where d.

`location_id`

=l.`location_id`

and city like

'_o%'

;# 案例6: 查詢每個城市的部門個數

# 知識點:可以加分組

select

count(*

)as 個數,city

from departments as d,locations as l

where d.

`location_id`

=l.`location_id`

group

by city

# 案例7: 查詢有獎金的每個部門的部門名和部門的領導編號和該部門的最低工資

# 知識點:可以加分組

select department_name,d.manager_id,

min(salary)

from departments as d,employees as e

where d.

`department_id`

=e.`department_id`

and commission_pct is

notnull

group

by department_name,manager_id;

# 案例8: 查詢每個工種名和員工的個數,並且按員工個數降序

# 知識點:可以加排序

select job_title,

count(*

)from employees as e,jobs as j

where e.

`job_id`

=j.`job_id`

group

by job_title

order

bycount(*

)desc

;# 案例9:查詢員工名、部門名和所在的城市

# 多表連線

select last_name,department_name,city

from employees as e,departments as d,locations as l

where e.

`department_id`

=d.`department_id`

and d.

`location_id`

=l.`location_id`

;# 1.非等值連線

# 案例1:查詢員工的工資和工資級別

# 建立等級表

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);

select salary,grade_level

from employees as e,job_grades as g

where salary between g.

`lowest_sal`

and g.

`highest_sal`

and g.

`grade_level`

='a'

;# 3、自連線

# 案例:查詢員工名和上級名稱

# 將一張表既看作員工表,又看作領導表

select e.employee_id,e.last_name,m.employee_id,m.last_name

from employees as e,employees as m

where e.manager_id=m.employee_id;

《學習》5連線查詢(高階查詢)

create table t tid int unsigned not null auto increment,tname varchar 30 primary key tid engine myisam auto increment 1 default charset utf8 insert in...

nodejs高階 6 連線MySQL資料庫

連線mysql資料庫需要安裝支援 npm install mysql 我們需要提前安裝按mysql sever端 建乙個資料庫mydb1 mysql create database mydb1 mysql show databases database information schema mysq...

08 1連線查詢 外連線 MySQL

外連線 應用場景 用於查詢乙個表中有,另乙個表中沒有的記錄 特點 1 外連線的查詢結果為主表中的所有記錄 如果從表中有和主表匹配的,則顯示匹配的值 如果從表中沒有和主表匹配的,則顯示null 外連線查詢的結果 內鏈結結果 主表中有而從表中沒有的記錄 2 左外連線 left join 左邊的是主表 右...