高階查詢,關聯查詢

2022-05-26 23:21:10 字數 3050 閱讀 1466

高階查詢

1.關聯查詢

作用:可以跨越多表查詢

--查詢出員工的名字和他所在部門的的名字

//錯誤

select first_name,name from s_emp,s_dept;

//錯誤的原因:

產生笛卡爾積(在查詢兩張表的時候,一張表中的資料與另一張表中的資料一一匹配),產生了冗餘資料。

//古老的寫法:

select first_name,name from s_emp,s_dept where s_emp.deop_id = s_dept.id;

//現在的寫法:

select e.first_name,d.name from s_emp e

join s_dept d on e.dept_id = d.id;

語法:select 列,列,列

from 表1 join 表2

on 表1外來鍵=表2主鍵

案例:-- 找出sales 部門的所有員工

select * from s_emp e

join s_dept d on e.dept_id=d.id

where d.name = 'sales';

-- 找出在 asia 地區工作的員工

select  * from s_emp e

join s_dept d on e.dept_id = d.id

join s_region r on d.region_id = r.id

where r.name  = 'asia';

--找出客戶'hamada sport' 的所有訂單號、費用、下訂日期

select o.id,o.total,o.date_ordered,c.name from s_ord o

join s_customer c on o.customer_id = c.id

where c.name ='hamada sport';

--找出所有在'asia'客戶的資訊

select * from s_region r

join s_customer c on c.region_id = r.id

where r.name  = 'asia';

練習:--查詢出客戶名字叫unisports的訂單資訊

select o.* from s_ord o

join s_customer c on o.customer_id = c.id

where lower(c.name) =lower('unisports');

--查詢出設在北美的的部門名稱

select s.name from s_dept s

join s_region r on s.region_id = r.id

where r.name = 'north america';

--查詢出在北美工作的員工姓名、工資、入職日期和職位

select e.first_name, e.salary ,e.start_date,e.title from s_emp e

join s_dept d on e.dept_id = d.id

join s_region r on d.region_id  =r.id

where r.name = 'north america';

--查詢出所有客戶名,及其訂單號

select c.name,o.id from s_customer c

left join s_ord o on o.customer_id = c.id

order by c.name

;2.外聯接

左外聯[left outer join]

以關聯的左邊為準,即使右邊沒有與之匹配的記錄,則左邊的記錄也要

出現在結果集中,右邊全部以null值顯示。

右外聯[right outer join]

以關聯的右邊為準,即使左邊沒有與之匹配的記錄,則右邊的記錄也要

出現在結果集中,左邊全部以null值顯示。

補充:全外聯,交叉外聯

--查詢出所有客戶名,及其訂單號

select c.name,o.id from s_customer c

left join s_ord o on o.customer_id = c.id

order by c.name

;--查詢所有訂單號,訂單費用以及訂單所對應的客戶名

select c.name,o.id,o.total from s_customer c

right join s_ord o on o.customer_id = c.id;

--找出womansport所購買的訂單資訊(訂單編號,費用,支付方式)

select o.id,o.total,o.payment_type from s_ord o

right join s_customer c on o.customer_id = c.id

where c.name = 'womansport';

--找出operations部門工作的員工名,工資,並且按照工資降序排列

select e.first_name,e.salary from s_emp e

left join s_dept d on e.dept_id  =d.id

where d.name  = 'operations'

order by e.salary desc;

注意:如何驗證:

--第一步:select * from s_emp;select  * from s_dept;檢視員工人數和部門個數,以及是否存在員工沒有分配部門的 情況,和是否存在新部門沒有員工的情況

--第二步:如果存在員工沒有分配部門,那麼以員工表為主表

--第三部:如果存在新部門沒有員工的情況,以部門表為主表

自關聯:

-- 查詢出所有的員工名以及員工的上司名

select e.firstname,m.fitstname from s_emp e

left join s_emp m on e.manager_id = m.id;

----

注:關聯的條件不一定總是做等值比較的。

MySQL查詢(關聯查詢)

一 資料庫關聯查詢 內連線查詢 inner join 查詢兩個表共有的資料,交集 select from tb1 inner join tb2 on 條件 所有有宿舍的學員 左表查詢 左關聯查詢 left join 查詢兩個表共有的資料,和左表所有的資料,左表有右表沒有的部分用null代替 sele...

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

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

表關聯查詢

一 內連線和外連線 內連線用於返回滿足連線條件的記錄 而外連線則是內連線的擴充套件,它不僅會滿足連線條件的記錄,而且還會返回不滿足連線條件的記錄,語法如下 oracle 1.select table1.column,table2.column from table1 inner left right...