Mysql關聯查詢

2021-09-24 04:08:10 字數 4519 閱讀 2949

七種結果:

(7)a ∪b- a∩b 或者 (a - a∩b) ∪ (b - a∩b)

如何實現?

(1)內連線

(2)外連線:左外連線、右外連線、全外連線(mysql使用union代替全外連線)

1.內連線:實現a∩b

select 字段列表

from a表 inner

join b表

on 關聯條件

where 等其他子句;

或select 字段列表

from a表 , b表

where 關聯條件 and 等其他子句;

**示例:
#查詢員工的姓名和他所在的部門的名稱

#員工的姓名在t_employee

#部門的名稱在t_department

select ename "員工的姓名"

,dname "部門名稱"

from t_employee inner

join t_department

on t_employee.did = t_department.did

select ename "員工的姓名"

,dname "部門名稱"

from t_employee , t_department

where t_employee.did = t_department.did

#查詢薪資高於20000的男員工的姓名和他所在的部門的名稱

select ename "員工的姓名"

,dname "部門名稱"

from t_employee inner

join t_department

on t_employee.did = t_department.did

where salary>

20000

and gender =

'男'

2.左外連線
#實現查詢結果是a

select 字段列表

from a表 left

join b表

on 關聯條件

where 等其他子句;

#實現a - a∩b

select 字段列表

from a表 left

join b表

on 關聯條件

where 關聯字段 is

null

and 等其他子句;

**示例:
#查詢所有員工的姓名和他所在的部門的名稱

select ename "員工的姓名"

,dname "部門名稱"

from t_employee left

join t_department

on t_employee.did = t_department.did

#查詢所有沒有部門的員工

select ename "員工的姓名"

,dname "部門名稱"

from t_employee left

join t_department

on t_employee.did = t_department.did

where t_employee.did is

null

3.右外連線
#實現查詢結果是b

select 字段列表

from a表 right

join b表

on 關聯條件

where 等其他子句;

#實現b - a∩b

select 字段列表

from a表 right

join b表

on 關聯條件

where 關聯字段 is

null

and 等其他子句;

**示例:
#查詢所有部門,以及所有部門下的員工資訊

select

*from t_employee right

join t_department

on t_employee.did = t_department.did

#查詢那些沒有員工屬於它的部門

select

*from t_employee right

join t_department

on t_employee.did = t_department.did

where t_employee.did is

null

4.用union代替全外連線
#實現查詢結果是a∪b

#用左外的a,union 右外的b

select 字段列表

from a表 left

join b表

on 關聯條件

where 等其他子句

union

select 字段列表

from a表 right

join b表

on 關聯條件

where 等其他子句;

#實現a∪b - a∩b 或 (a - a∩b) ∪ (b - a∩b)

#使用左外的 (a - a∩b) union 右外的(b - a∩b)

select 字段列表

from a表 left

join b表

on 關聯條件

where 關聯字段 is

null

and 等其他子句

union

select 字段列表

from a表 right

join b表

on 關聯條件

where 關聯字段 is

null

and 等其他子句

**示例:
#查詢所有員工,所有部門,包括沒有員工的部門,和沒有部門的員工

select

*from t_employee left

join t_department

on t_employee.did = t_department.did

union

select

*from t_employee right

join t_department

on t_employee.did = t_department.did

#查詢那些沒有部門的員工和所有沒有員工的部門

#沒有部門的員工

select

*from t_employee left

join t_department

on t_employee.did = t_department.did

where t_employee.did is

null

union

#所有沒有員工的部門

select

*from t_employee right

join t_department

on t_employee.did = t_department.did

where t_employee.did is

null

5.自連線

兩個關聯查詢的表是同一張表,通過取別名的方式來虛擬成兩張表

select 字段列表

from 表名 別名1

inner

/left

/right

join 表名 別名2

on 別名1.關聯字段 = 別名2的關聯字段

where 其他條件

**示例:
#查詢員工的編號,姓名,薪資和他領導的編號,姓名,薪資

#這些資料全部在員工表中

#把t_employee表,即當做員工表,又當做領導表

#領導表是虛擬的概念,我們可以通過取別名的方式虛擬

select emp.eid "員工的編號"

,emp.ename "員工的姓名"

,emp.salary "員工的薪資"

, mgr.eid "領導的編號"

,mgr.ename "領導的姓名"

,mgr.salary "領導的薪資"

from t_employee emp inner

join t_employee mgr

#t_employee emp:如果用emp.,表示的是員工表的

#t_employee mgr:如果用mgr.,表示的是領導表的

on emp.mid = mgr.eid

#表的別名不要加"",給列取別名,可以用"",列的別名不使用""也可以,但是要避免包含空格等特殊符號。

mysql關聯查詢去重 MySQL 關聯查詢

mysql 關聯查詢 sql資料分析 1週前 mysql 關聯查詢 前面,我們介紹的都是單錶查詢 就是只從一張表中獲取資料 而實際應用的時候,我們都會同時查詢多張表,這裡,我們就介紹下,多表關聯查詢的使用。sql join 用於根據兩個或多個表中的列之間的關係,從這些表中查詢資料 前置知識 主鍵 p...

mysql關聯查詢

在程式開發時,不可避免的要用到檢視,首先我們來看看檢視到底有什麼作用 簡單性 看到的就是需要的。檢視不僅可以簡化使用者對資料的理解,也可以簡化他們的操作。那些被經常使用的查詢可以被定義為檢視,從而使得使用者不必為以後的操作每次制定全部的條件。安全性 通過檢視用固話只能查詢和修改他們所能見到的資料。資...

mysql 關聯查詢

inner join 只取兩張表的交集的部分,join預設為inner join left join 取左表的所有部分,並且取出與右表關聯上的資料 同left join的作用,只是驅動錶換了位置 全外連線,mysql目前沒有,可以用union代替,如 select e.empname,d.deptn...