MySQL_05-Select查詢詳解,mysql_05-select

來源:互聯網
上載者:User

MySQL_05-Select查詢詳解,mysql_05-select

SQL查詢詳解

1.Orderby

a) Order by 欄位 asc | desc

b) 允許多欄位排序: 先按第一個欄位排序,如果不能區分,再使用第二個欄位排序;以此類推

2.Limit(限制獲得的記錄數量)

a) 文法: limit offset, row_count  (offset:位移量,下標從0開始;row_count:總記錄數)。例如:limit 2,3 表示從第二條開始,取三條(即第2條到第4條記錄)

3.Distinct

a) 去除重複記錄。  

b) 文法: select distinct 欄位 from .......

4.Union

a) 將兩個不同關係中的查詢聯合起來返回

b) 例:查詢1班和2班的授課老師

(Select teacher from t_class where class_id=’1’)

Union (all)

(Select teacher from t_class where class_id=”2”)

c) 注意:如果union的結果有重複資料,會自動消除重複,查詢結果會遺失資料!解決:通過union的選項all來實現。  Union all...

d) 注意:union操作中子語句結果的排序問題

i. 將子語句包裹在括弧內,非必需,但是便於閱讀,邏輯更加清晰!

ii. 子語句的order by只有配合limit時才會生效!沒有limit時排序是不起作用的!

e) 對union的最終結果進行排序(在語句最後添加order by ... 即可)

(Select teacher from t_class where class_id=’1’)

Union (all)

(Select teacher from t_class where class_id=”2”)

Order by stu_number desc;

 

5.子查詢(*)

a) 子查詢語句用括弧括起來

b) 子查詢分類

i. 子查詢出現的位置:where型(出現在where後)、from型、exists型

ii. 子查詢的返回值形式: 單一值(標量)、列、表

c) 子查詢例子

i. 從教師表中查出授課天數最多的老師的姓名:Select t_name from t_teacher where teach_days=(select max(teach_days) from t_teacher);  

注意:使用下列語句也可能達到效果

Select t_name from t_teacher order by teach_days desc limit 1;

但是,可能出現問題。如果有幾個老師的授課天數都最大並且一樣,這樣會漏掉結果!

ii. 子查詢返回集合: in ,not in, any, all

iii. 若子查詢返回結果是一行(行子查詢一般使用不多,但是要掌握!!!):使用(filed1,field2,...)這種形式構建一行來和子查詢返回的結果進行比較!!!

例:要求從教師表中查詢出和老師張三性別相同,並且也教過張三所教班級的其他老師的資訊。

Select t_name, gender, age, course_name from t_teacher

 where (gender, course_name) =   //構建一行於子查詢結果比較(使用最多的是 = 和 in)

(select distinct gender, course_name from t_teacher where t_name=’張三’ and course_name=’MySql資料庫教程’);

iv. 子查詢返回一個表(通常用於from型子查詢,即子查詢返回的結果在from關鍵字後使用)。將一個複雜的邏輯分步處理。

v. Exists子查詢: exists(subquery),不提供資料,相當於一個布林運算式,判斷是否擷取到了資料。

 

 

6.串連查詢(*)

a) Inner:資料內部的串連,要求參與串連的資料都存在才能執行的串連。

i. Inner join 內串連

ii. Cross join 交叉連結(笛卡爾連結):沒有串連條件,結果集的大小等於兩個參與串連的表的結果集笛卡爾乘積。

b) Outer:參與串連的一個或多個資料不真實存在的串連。

i. Left [outer] join 左外串連(開發中使用最多!!!): 左外串連時,如果出現左邊表資料連線不到右邊表的資料,則左邊表的資料最終被保留在結果內,右表對應欄位填充NULL; 而如果出現右邊表的資料連線不到左邊表資料的情況,右邊資料被丟棄。

ii. Right [outer] join 右外串連

iii. Full join 全串連(暫不支援)

c) Natural 自然串連(natural join, natural left join, natural right join): 通過mysql自己的判斷完成串連過程,不需要指定串連條件。Mysql自動使用多表內相同的欄位作為串連條件。

Select * from t_one natural join t_two;

d) 串連條件

i. On: 串連條件

ii. Where: 對串連的資料過濾.

iii. Using: 串連條件,只有當兩個表中串連的欄位相同時才使用using,一般情況下很少使用,通常使用on。例如有兩個表:t_student_main_info(學生常用屬性)和t_student_other_info(學生不常用資訊),它們的主鍵名稱相同,都叫id,此時才能使用using關鍵字。 Select * from t_student_main_info join t_student_other_info using(id);

 

註:串連操作支援多表串連,可以一直無限串連下去,但是極少使用。例如:

Select t_two.* ,t_three.* from t_one left join t_two on ... Left join t_three on ... where...

 

兩個例子:

1.假設兩個表分別為team和match

Id

Team_name

2

Php0331

5

Php0228

1

Php0115

7

Php0505

 

id

Host_id

Guest_id

time

result

1

2

1

10:00

34:28

2

2

7

15:00

35:45

3

 

 

 

 

4

 

 

 

 

 

要求查詢得到如下結果:

主隊

客隊

比賽時間

比賽結果

Php0331

Php0115

10:00

34:28

Php0331

Php0505

15:00

35:45

 

由於結果中每行記錄有兩個team_name,所以需要串連team表2次!!!

Match left join team on match.host_id=team.id left join team on match.guest_id=team.id

 

2.自身串連

有如下課程表course

cno

cname

cpno

credit

1

資料庫

5

4

2

數學

 

2

3

資訊系統

1

4

4

作業系統

6

3

5

資料結構

7

4

6

資料處理

 

2

7

C語言

6

4

要求查詢每門課的間接先修課:

Select first.cno, second.cpno from course first, course second where first.cpno=second.cno;

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.