MySQL基礎 連線查詢

2021-10-06 20:49:34 字數 4587 閱讀 6219

含義:又稱多表查詢,當查詢的字段來自多個表時,就會用到連線查詢

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

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

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

分類:

按功能分類

①內連線(包括等值連線、非等值連線和自連線)

②外連線(包括左外連線、右外連線和全外連線)

③交叉連線

select name,boyname from boys,beauty;
這樣寫會發生笛卡爾乘積現象,應寫為下面這種:

select name,boyname from boys,beauty

where beauty.boyfriend_id=boys.id;

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

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

③多表的順序沒有要求;

④一般需要為表起別名;

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

案例1:查詢女神名和對應的男神名

select name,boyname from boys,beauty

where beauty.boyfriend_id=boys.id;

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

select last_name,department_name

from employees,departments

where employees.department_id=departments.

`department_id`

;

好處:提高語句的簡潔度,區分多個重名的字段

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

案例:查詢員工名、工種號、工種名

select last_name,e.job_id,job_title

from employees as e,jobs j #生成虛擬檢視

where e.

`job_id`

=j.`job_id`

;

select e.last_name,e.job_id,j.job_title

from jobs j,employees e

where e.

`job_id`

=j.`job_id`

;

案例1:查詢有獎金的員工名和部門名

select last_name,department_name

from employees e,departments d

where e.

`department_id`

=d.`department_id`

and e.

`commission_pct`

isnot

null

;

案例2:查詢城市名中第二個字元為o的對應的部門名和城市名

select department_name,city

from departments d,locations l

where d.

`location_id`

=l.`location_id`

and city like

'_o%'

;

案例1:查詢每個城市的部門個數

select

count(*

) 個數,city

from departments d,locations l

where d.

`location_id`

=l.`location_id`

group

by city;

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

select department_name,d.manager_id,

min(salary)

from departments d,employees e

where d.

`department_id`

=e.`department_id`

and commission_pct is

notnull

group

by department_name,d.manager_id;

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

select job_title,

count(*

)from employees e,jobs j

where e.

`job_id`

=j.`job_id`

group

by job_title

order

bycount(*

)desc

;

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

select last_name,department_name,city

from employees e,departments d,locations l

where e.

`department_id`

=d.`department_id`

and d.

`location_id`

=l.`location_id`

;

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

select salary,grade_level

from employees e,job_grades g

where e.

`salary`

between g.

`lowest_sal`

and g.

`highest_sal`

;

create table:

由於原資料庫中沒有工資級別的表,所以要進行建立,create table語句的語法結構如下:

create

table

表(列名1

>

資料類該列所需約束(可選)

>

,列名2

>

資料類該列所需約..

..);

進行工資級別表的建立:

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

*from job_grades;

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

select e.employee_id 員工,e.last_name,m.employee_id 上級,m.last_name

from employees e,employees m#e為員工表,m為領導表

where e.manager_id=m.employee_id;

MySQL表連線查詢基礎

當需要同時顯示多個表的字段時,就可以用表連線來實現這樣的功能。表連線分為內連線和外連線。內連線僅選出兩張表中互相匹配的記錄,而外連線會選出其他不匹配的記錄。以下面兩張表為示範 表名 teacher table 建表語句 create table teacher table teacher id in...

mysql連線查詢例項 MySQL連線查詢例項詳解

建立表suppliers create table suppliers s id int not null auto increment,s name char 50 not null,s city char 50 null,s zip char 10 null,s call char 50 not...

mysql連線查詢例項 MySQL連線查詢例項詳解

建立表suppliers create table suppliers s id int not null auto increment,s name char 50 not null,s city char 50 null,s zip char 10 null,s call char 50 not...