1 / 90

S tructured Q uery L anguage

S tructured Q uery L anguage. Select 指令介紹. 資料查詢( 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] ]. Predicate.

zelda
Télécharger la présentation

S tructured Q uery L anguage

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation 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、產品名稱為「新產品一」以及單價為195 • insert into products values (120, "HHT", "新產品一",195)

  36. 新增資料錄 • 先複製distributors 為distributors1並修改其資料內容並將distributors1之內容新增到distributors • insert into distributors select * from distributors1 • 只增加boss_name之資料到distributors • insert into distributors select boss_name from 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;

More Related