1 structured query language. 2 select 指令介紹 3 資料查詢( select 語法介紹) select...
Post on 21-Dec-2015
228 views
TRANSCRIPT
1
Structured Query Language
2
Select 指令介紹
3
資料查詢( Select 語法介紹)SELECT [predicate] select_list
[ INTO new_table]
FROM <table list>
[WHERE <row selection specification>]
[GROUP BY <grouping specification>]
[HAVING search_condition]
[ORDER BY order_expression [ASC|DESC] ]
4
Predicate
ALL :
select all * from suppliers
select * from suppliers
DISTINCT: 省略重複資料之欄位DISTINCEROW : 省略重複資料的前後記錄列TOP N [PERCENT]: 傳回記錄中最前面的 n 個或
n% 數目的記錄
5
選取所有的欄位範例 1 、查詢供應商表格所有資料錄所有欄位的資料
select * from suppliers
6
選取特定的欄位範例 2 、查詢所有經銷商的公司名稱與負責人姓名
select company, boss_name from distributors
7
選取唯一的數值範例 3 、查詢已經下訂單的經銷商號碼,但去除重複的經銷商號碼
Select distinct distri_num from orders Select distinctrow distri_num from orders
8
選取子字串範例 4 、查詢所有經銷商的電話區域碼select left(phone, 2) from distributors
9
練習 1練習 1 、我們於範例 4 中雖然查詢出所有經銷商的電話區域碼,但是重複資料甚多,請設計一 SQL 敘述顯示單一區域碼
10
選取特定的資料錄範例 5 :查詢供應商代碼為 HHT 供應商所有的相關資料
Select * from suppliers where supp_code="HHT"
11
排除特定的資料錄範例 6 :查詢供應商代碼不為 HHT 的供應商所有的相關資料
Select * from suppliers where supp_code <> "HHT"
12
選取介於某一範圍的資料範例 7 :查詢單價介於 20 元與 30 元之間所有產品的產品名稱與單價資料
Select description, unit_price from products
where unit_price between 20 and 30
13
練習 2
•練習 2 :使用’ (‘ 、 ‘ )’ 、 ‘>=’ 、 ‘ <=’ 以及 AND 邏輯運算元,將範例 7 改寫,查詢單價介於 20 元與 30 元之間所有產品名稱與單價資料。
14
選取屬於某一子集合的資料範例 8 :查詢經銷商號碼為 1018 、 1014 、
1006 、 1001 或 1027 的經銷商資料,我們所關心的資訊為經銷商號碼、負責人姓名以及公司名稱。
Select distri_num, boss_name, company from distributors where distri_num in (1018,1014,1006,1001,1027)
15
識別 NULL 值範例 9 :查詢尚未付款客戶的所有資料Select * from orders where paid_date is null
範例 10 :查詢已付款客戶的所有資料Select * from orders where paid_date is not null
16
練習 3
練習 3 :請設計一 SELECT 敘述,查詢於 06/01/1996 以後訂貨的所有訂單資料。
17
搜尋完全相等的字串範例 11 :查詢「傑印電腦」經銷商的電話號碼與地址
select phone, address from distributors where company = " 傑印電腦 "
18
搜尋子字串範例 12 :查詢某一公司的地址,只記得該公司在台北市,而且電話號碼前兩碼為 27
Select company, phone, address from distributors where phone like "0227*"
19
在 WHERE 子句中使用子字串範例 13 :查詢電話區域碼為 03 所有經銷商的公司名稱與電話號碼
Select company, phone from distributors where left(phone, 2) = "03"
20
練習 4
練習 4 :查詢位於台北市以外所有經銷商的公司名稱、電話號碼以及地址資料。
21
練習 5練習 5 :查詢一客戶的電話號碼,但是只記得該公司為某某科技,而且公司名稱中有一個「美」字。
22
使用 ORDER BY 子句範例 14 :請查詢所有產品的產品號碼、供應商代碼、產品名稱以及單價資訊,請依單價由小至大加以排序( Sorting )
Select prod_num, supp_code, description, unit_price from products order by unit_price
23
使用 ORDER BY 子句範例 15 :請查詢所有點陣式印表機的產品號碼、供應商代碼、產品名稱以及單價資訊,請依供應商代碼由大至小( Descending )加以排序
Select prod_num, supp_code, description, unit_price from products where description = " 點陣式印表機 " order by supp_code desc
24
練習 6練習 6 :查詢位於台北縣市、負責人姓李或陳的經銷商資料,列出公司名稱、負責人姓名以及所在縣市代碼。以縣市代碼升冪排序
25
使用 GROUP BY 子句 Syntax : group by column#1, column#2, …
[having expression]
distri_num from orders group by distri_num
26
使用 GROUP BY 子句範例 16 :查詢位於各供應商之供應產品總金額。以金額升冪排序。
Select supp_code, sum(total_price) as Tprice from items group by supp_code order by sum(total_price)
27
使用 HAVING 子句請查詢每一訂單的總金額大於 500 並依總金額由小至大排序
select order_num, sum(total_price) as totalprice from items group by order_num having sum(total_price) > 500 order by sum(total_price)
28
範例 17 :林老闆擬將所有產品的價格調高 5% ,但是在正式調漲之前,他希望能看一下調漲後各產品的價位,並且以產品單價由小至大排序。
Select prod_num, supp_code, description, unit_price * 1.05 from products order by unit_price
使用算數運算式
29
* 字元 : 符合任何字元個數– wh* which, where, when …
? 字元 : 符合任何單一字母的字元 [charlist] : 符合任何 charlist 中的單一字元
– C[ae]ll Call 和 Cell [!charlist] : 符合任何不在 charlist 中的單一字元
– C[!ae]ll 不包含 Call 和 Cell C[a-e]ll Call 和 Cell
– [a-z], [A-Z] 不可 [z-a] 或 [Z-A]# 字元 : 符合任何單一數值的字元
– 1#3 112,123,133…
使用萬用字元
30
select * from suppliers where supp_name like "[ 寧和 ]* "
select * from suppliers where supp_name like "[! 寧 ]* "
select * from products where supp_code like "[A-Z]F[A-Z] "
select * from orders where order_num like "1#3"
使用萬用字元
31
為虛擬欄位命名範例 18 :由上一範例得知,在輸出結果中,虛擬欄位是沒有欄位名稱的。為了易於閱讀起見,請為該虛擬欄位加上欄位名稱 new_price 。
Select prod_num, description, unit_price * 1.05 as new_price from products order by unit_price
32
為虛擬欄位命名範例 19 :修改範例 18 中的 SQL 敘述,輸出標題分別為「產品號碼」、「產品名稱」以及「產品新單價」,並且以產品新單價由小至大排序
select prod_num as 產品號碼 , description as 產品名稱 , unit_price * 1.05 as 產品新單價 from products order by unit_price * 1.05
33
搜尋單一長度的任意字串範例 20 :請查詢供應代碼第一、第三位分別為 H 與 T 所有供應商的資料
select * from suppliers where supp_code like "H?T"
34
新增資料錄 Syntax :
insert into table_name values (column#1
value, column#2 value, … )
insert into table_name defaults values
insert into table_name1 select column#1
from table_name2 where …..
35
新增資料錄範例 21 :請在 products 表格中新增一筆資料錄,其產品號碼為 120 、供應商代碼為 HHT 、產品名稱為「新產品一」以及單價為 195insert into products values (120, "HHT", " 新產品一 ",195)
36
新增資料錄先複製 distributors 為 distributors1 並修改其資料內容並將 distributors1 之內容新增到 distributors
insert into distributors select * from distributors1
只增加 boss_name 之資料到 distributorsinsert into distributors select boss_name fro
m distributors1
37
刪除資料錄 Syntax :
delete [from] table_name
where column_name = value請刪除經銷商 1018 的資料錄delete from distributors where distri_num =1018
38
修改資料錄的內容 Syntax :
update table_name set col#1 = value1,
col#2 = value2, col#3 = value3, …
[where …..] Example : update products set unit_price = unit_price*1.03 update products set unit_price = unit_price*1.03 where prod_num=310
39
修改資料錄的內容範例 22 :請將經銷商 1018 的公司名稱改為「新公司一」、電話號碼改為 023334444
update distributors set company =" 新公司一 ", phone= "023334444" where distri_num = 1018
40
練習 7
練習 7 :將供應商編號為‘ FYC’ 之供應商所供應之每一項產品的供應價打九五折
41
範例 23 : Parameters [Enter Price] currency; select * from items where total_price > [Enter Price]
42
範例 24 : Parameters [Order_number] integer;
select * from items where order_num =
[Order_number]
43
範例 25 : Parameters [orderdate] datetime;
select * from orders where order_date >= [orderdate]
44
範例 26 : Parameters [Supply_code] text;
select * from items where supp_code =
[Supply_code]
45
在一 select 敘述中使用子查詢
範例 27 :請查詢與「負責人為張江悅的經銷商」在同一縣市的所有經銷商的資料
select * from distributors where hsienshi_code =(select hsienshi_code from distributors where boss_name = " 張江悅 ")
46
請列出和供應商代碼 (supp_code) 為 WCC 供應相同產品 (description) 之供應商代碼 (supp_code) 及供應商名稱 (supp_name)
SELECT products.supp_code, supp_name, description from products inner join suppliers on products.supp_code=suppliers.supp_code where description = (select description from products where supp_code="WCC")
47
練習 8請查詢所有訂單項單項價大於所屬訂單最小單項總價兩倍的訂單項目資料。
48
練習 9
請查詢每一經銷商的經銷商名稱與最早訂貨日期
49
TRANSFORM
TRANSFORM aggfunction selectstatement PIVOT pivotfield [IN (value1[, vlaue2[, …]]]
50
範例 28 :請找出員工之各種請假之時數總合TRANSFORM Sum(AbsHour)
SELECT Name
FROM 員工基本資料 INNER JOIN 員工請假資料 ON 員工基本資料 .EmpNo = 員工請假資料 .EmpNo
GROUP BY Name
PIVOT AbsType;
51
TRANSFORM Sum(items.total_price) AS total_price 之總計 SELECT orders.order_num FROM (distributors INNER JOIN orders ON distributors.distri_num = orders.distri_num) INNER JOIN items ON orders.order_num = items.order_num GROUP BY orders.order_num
PIVOT distributors.distri_num;
52
Union 特性不同資料表或使用不同的方法擷取相同格式的資料,希望將所選取的資料整合在一起
輸出資料需具有相同的資料型態輸出資料的欄位必須一致Union 兩邊的欄位輸出順序須一致會自動移除重覆之資料
53
UnionSyntax : SELECT column_1, …., column_n FROM table_1, … , table_n WHERE column_name comparsion_op value [GROUP BY ...][HAVING][ORDER BY] UNION [ALL] SELECT column_1, …., column_n FROM table_1, … , table_n WHERE column_name comparsion_op value [GROUP BY ...][HAVING][ORDER BY]
54
使用 union 運算元範例 29 :請查詢單價大於 25 元或是已訂貨項目中數量小於 2 的產品資料,列出產品號碼與供應商代碼。
Select prod_num, supp_code from products
Where unit_price <25 union Select prod_num, supp_code from items
Where quantity < 2
55
union範例 30 :select company, address, phone from distributorsselect prod_num,supp_code, total_price from itemsselect company, address, phone from distributors union select prod_num,supp_code, total_price from items
56
Embedding Functions
57
LEN 函數範例 31 :請計算經銷商 1001 地址的的字串長度
select address, len(address) as 長度 from distributors where distri_num = 1001
58
常用的時間函數 day 函數 month 函數 year 函數 now 函數 date 函數 weekday 函數
59
day 函數擷取一日期的日數select order_num,day(order_date) from ord
ers where order_num =102
select order_num,day(order_date) from orders where order_num like "10#“
60
month 函數擷取一日期的月數 select order_num,month(order_date) from orders where order_num =102select order_num,month(order_date) from orders where order_num like "10#"請查詢每一訂單號碼的下單月份select order_num, month(paid_date) as month from orders
61
year 函數請擷取一日期的年份
select order_num,year(order_date) from orders where order_num =102
62
weekday函數
select order_num, weekday(order_date) from orders where order_num =102
63
練習 10李老板希望能得知與本月相同月份訂貨的訂單號碼與訂貨日期。
64
常用的集總函數count 函數sum 函數avg 函數max 函數min 函數STDEV, STDEVP 函數VAR, VARP 函數
65
count
請查詢本公司總共銷售多少種產品 select count(*) from products
select count(*) from distributors
select count(*) from suppliers
66
sum
請查詢 HHT 供應商已訂貨的總金額select sum(total_price) from items where supp_code="HHT"
67
avg
請查詢所有供應商所供應貨品的平均單價
select supp_code as 供應商代碼 , avg(unit_price) as 平均單價 from products group by supp_code
68
max
請查詢每一供應商產品的最高單價
select supp_code, max(unit_price) from products group by supp_code
69
min
請查詢每一經銷商的最早訂貨日期
select distri_num, min(order_date) from orders group by distri_num
70
練習 11請設計一敘述,列出超過一筆訂單的所有經銷商的經商號碼與訂單總數
71
Outline Join Inner Join Outer Join
Left Join and Right Join
Union
72
Join的特性兩個以上資料來源的彙整與連結查詢需設定資料來源的連結條件 (Join condition)使用 Select 指令的 join 關鍵字進行資料來源的連結 , 同時使用了 on 設定了連結控制條件
一般連結會產生的結果為多欄位之 Result Set
73
Join的類型
內部合併( Inner Join ) 外部合併( Outer Join )
74
Inner Join連結與比對兩個資料來源之間的連結控制指定的欄位內容 , 是否兩邊同時吻合
不能在連結控制條件中 , 使用具有 NULL 值的欄位進行比對
可以加上篩選條件 On 進行記錄的篩選Syntax : Select <output fileds> From <tables> I
nner Join <tables> On <conditions>
75
範例 32
建立一查詢 , 可以讓我們看到所有訂單之編號、訂貨日期、經銷商之編號、經銷商之公司名稱以及付款日期
76
範例 32 解答SELECT orders.order_num, orders.order_date, di
stributors.distri_num, distributors.company, orders.paid_date FROM orders INNER JOIN distributors ON orders.distri_num = distributors.distri_num;
SELECT orders.order_num, orders.order_date, distributors.distri_num, distributors.company, orders.paid_date FROM orders, distributors where orders.distri_num = distributors.distri_num;
77
練習 12
請查詢每一經銷商所有的供應資料,列出經銷商號碼、公司名稱、電話號碼、 訂單編號,訂貨日期並以經銷商號碼由小至大排序。
78
在 select 敘述中使用多個資料表
請查詢 1004 經銷商所有訂單的資料,列出經銷商號碼、訂單號碼、項目號碼以及單項總價。SELECT distributors.distri_num, orders.order_num, items.item_num, items.total_price FROM distributors INNER JOIN (orders INNER JOIN items ON orders.order_num = items.order_num) ON distributors.distri_num = orders.distri_num and distributors.distri_num =1004;select distributors.distri_num, orders.order_num, item_num, total_price from distributors, orders, items where distributors.distri_num=orders.distri_numand orders.order_num = items.order_num and distributors.distri_num =1004
79
Outer Join
80
Outer Join可以指定某一個資料來源為主 , 連結至另一個資料來源時 , 若兩邊記錄無法同時吻合時 , 可以將主要的資料來源之不對應連結的記錄完全呈現 , 對應不到的連結資料之欄位經均以 NULL 顯示
Left OuterRight Outer
81
建立一查詢 , 可以讓我們看到所有訂單之編號、訂貨日期、貨品編號、數量以及總價 ( 檢查不合理之訂單 ) select orders.order_num, orders.order_date, items.prod_num, items.quantity, items.total_price from orders left join items on orders.order_num=items.order_num
範例 33
82
請查詢每一經銷商的下單明細(要包含尚未下單的經銷商),列出經銷商號碼、負責人姓名以及訂單號碼
select distributors.distri_num, boss_name, order_num from distributors left outer join orders on distributors.distri_num = orders.distri_num
範例 34
83
練習 13
請查詢每一供應商已售出貨品的總金額,列出供應商公司名稱與售出貨品的總金額。 ( 並查出是否有供應商沒有出售任何貨品 )
84
Self Join
85
Self Join
同一資料表可以和自己做 JOIN常應用在同一資料表內不同資料間對同一欄位作比較
86
一表格與自己作合併請查詢供應相同產品的供應商代碼,列出產品名稱與供應商代碼,以成對的方式列出。
SELECT products.supp_code, products_1.supp_code, products_1.description FROM products INNER JOIN products products_1 ON products.supp_code <> products_1.supp_code and products.description = products_1.description GROUP BY products.supp_code, products_1.supp_code, products_1.description;
87
練習 14請查詢價格低於 300 元之相同產品 ( 兩個以上 ) 的產品名稱與產品價格。
88
在一 select 敘述中使用子查詢
請查詢與「負責人為張江悅的經銷商」在同一縣市的所有經銷商的資料
select * from distributors where hsienshi_code =
(select hsienshi_code from distributors where boss_name = " 張江悅 ")
89
練習 15
請查詢所有訂單項單項價大於所屬訂單最小單項總價兩倍的訂單項目資料。
90
練習 16
請查詢每一經銷商的經銷商名稱與最早訂貨日期