2 多表查詢

2021-09-20 13:15:30 字數 4228 閱讀 1387

笛卡爾積錯誤:多表查詢必須有連線條件,否則會產生笛卡爾積錯誤,得到資料條數=每個表資料條數的積

select last_name,employees.department_id, department_name from employees, departments
注意:查詢的字段在多個表同時存在必須帶表名

內連線:合併具有同一列的兩個以上的表的行, 結果集中不包含乙個表與另乙個表不匹配的行

等值連線(兩個表)

select last_name,e.department_id, department_name from employees e, departments d where e.department_id = d.department_id
等值連線(多個表)

select last_name,e.department_id, department_name,l.city from employees e, departments d, locations l where e.department_id = d.department_id and d.location_id = l.location_id
連線n個表,需要n-1個連線條件

非等值連線:

select employee_id,last_name,salary,grade_level from employees e,job_grades j where e.salary between j.lowest_sal and j.highest_sal
自連線:

查詢公司中員工『chen』的manager資訊:

select emp.last_name,manager.last_name,manager.salary,manager.email from employees emp, employees manager

where emp.manager_id = manager.employee_id and lower(emp.last_name)

='chen'

外連線:兩個表在連線過程中除了返回滿足連線條件的行以外還返回左(或右)表中不滿足條件的行 ,這種連線稱為左(或右) 外連線

左外連線:

select last_name,e.department_id, department_name from employees e, departments d where e.department_id = d.department_id(

+)

右外連線:

select last_name,e.department_id, department_name from employees e, departments d where e.department_id(+)

= d.department_id

sql:1999語法:

自然連線(natural join):自動連線兩個表中所有相同的字段,查詢的欄位名不能使用限定詞

select last_name,department_id, department_name from employees e natural

join departments d

useing:查詢字段不能使用限定詞,兩個表的連線欄位名和資料型別必須相同才能使用using

select last_name,department_id, department_name from employees e join departments d using

(department_id)

內關聯:join on格式

select last_name,e.department_id, department_name from employees e join departments d on e.department_id = d.department_id
select last_name,e.department_id, department_name,l.city from employees e 

join departments d on e.department_id = d.department_id

join locations l on d.location_id = l.location_id

外關聯:outer可以省略

左外關聯:

select last_name,e.department_id, department_name from employees e left

outer

join departments d on e.department_id = d.department_id

右外關聯:

select last_name,e.department_id, department_name from employees e right

outer

join departments d on e.department_id = d.department_id

全外關聯:

select last_name,e.department_id, department_name from employees e full

outer

join departments d on e.department_id = d.department_id

練習題:

顯示所有員工的姓名,部門號和部門名稱。

select last_name,e.department_id,department_name

from employees e, departments d

where e.department_id = d.department_id(+)

;

查詢90號部門員工的job_id和90號部門的location_id

select

distinct job_id,location_id

from employees e left

join departments d

on e.department_id = d.department_id

where d.department_id =

90

選擇所有有獎學金的員工的last_name,department_name,location_id,city

select last_name,department_name,d.location_id,city

from employees e

join departments d on e.department_id = d.department_id

join locations l on d.location_id = l.location_id

where e.commission_pct is

notnull

;

選擇city在toronto工作的員工的last_name,job_id,department_id,department_name

select last_name,job_id,e.department_id,department_name

from employees e

join departments d on e.department_id = d.department_id

join locations l on d.location_id = l.location_id

where l.city =

'toronto'

;

自連線練習

select e1.last_name "employees"

,e1.employee_id "emp#"

,e2.last_name "manager"

,e2.employee_id "mgr#"

from employees e1,employees e2

where e1.manager_id = e2.employee_id(+)

;

多表查詢 多表查詢 多表查詢

查詢語法 select 列表名稱 from 表明列表 where 笛卡爾積 有兩個集合a,b,取這兩個集合的所有組成情況 要完成多表查詢,需要消除無用的資料 多表查詢分類 1 內連線查詢 1 隱式內連線 使用where消除無用的資料 例子 select t1.name,t1.gender,t2.na...

mysql 多表查詢or MySQL 多表查詢

前期準備 建表create table dep id int,name varchar 20 create table emp id int primary key auto increment,name varchar 20 enum male female not null default ma...

查詢 多表查詢。。。

此時你得使用鏈結條件。通過存在於相對應列中的公共值,乙個表中的資料可以被另乙個表的資料鏈結,通常都是主鍵和外來鍵進行鏈結。一般鏈結條件寫在where子句裡。select empno,emp.deptno,loc from emp,dept where emp.deptno dept.deptno 對...