Group by
Having
Distinct
Count
Min
Max
Avg
Sum
where對from出來的表數(shù)據(jù)進行篩選
group
by是再where執(zhí)行之后才執(zhí)行
having對group by的結(jié)果進行篩選
SQL的執(zhí)行順序:
[if !supportLists]1.[endif]From:視圖1
[if !supportLists]2.[endif]Where:視圖2
[if !supportLists]3.[endif]Group
by:視圖3
[if !supportLists]4.[endif]Having:視圖4
[if !supportLists]5.[endif]Select:視圖5
[if !supportLists]6.[endif]Distinct:視圖6
[if !supportLists]7.[endif]Order
by:視圖7
1.“增刪改查”哪一種?
select*
2.從哪些表能夠找全字段?
from表1 a,表2 b
3.是否是多表?
wherea.字段1=b.字段2
4.是否有字段約束?
and字段名
5.什么約束?
=字段值
6.是否有非全表的聚合函數(shù)?
group by
7.誰的聚合函數(shù)(按哪些字段進行聚合)?
字段名1,字段名2
8.將展現(xiàn)字段改為聚合字段
select字段名1,字段名2
9.對聚合函數(shù)是否有約束?
having
10.什么約束?
聚合函數(shù)>條件
12.最終要展現(xiàn)什么字段?
字段+聚合函數(shù)
13.是否去重?
Distinct
11.是否有排序?
order by
Student(S#, Sname,Sage,Ssex)學(xué)生表
Course(C#,Cname,T#)課程表
SC(S#,C#,score)成績表
Teacher(T#,Tname)教師表
問題:
[if !supportLists]1、[endif]查詢“001”課程比“002”課程成績高的所有學(xué)生的學(xué)號:
[if !supportLists]2、[endif]查詢平均成績>60分的同學(xué)的學(xué)號和平均成績
SelectS#,avg(score)fromsc tgroup by S# having avg(score) >60
查詢“001”課程比“002”課程成績高的所有學(xué)生的學(xué)號
Selecta.S#from SC a , SC b where a.S#=b.S# and a.C#=’001’and b.C#=’002’
[if !supportLists]1.[endif]“增刪改查”哪一種?
查詢“001”課程比“002”課程成績高的所有學(xué)生的學(xué)號
select*
[if !supportLists]2.[endif]從哪些表能夠找全字段?
查詢“001”課程比“002”課程成績高的所有學(xué)生的學(xué)號
Select*fromSC
a, SC b
3.是否是多表?
Select*from SC a, SC bwherea.sno=b.sno
4.是否有字段約束?
Select*from SC a, SC bwherea.sno=b.snoand a.C#=’001’ and b.C#=’002’
5.什么約束?
=字段值
6.是否有非全表的聚合函數(shù)?
group by
7.誰的聚合函數(shù)(按哪些字段進行聚合)?
字段名1,字段名2
8.將展現(xiàn)字段改為聚合字段
select字段名1,字段名2
9.對聚合函數(shù)是否有約束?
having
10.什么約束?
聚合函數(shù)>條件
11.是否有排序?
order by
12.按什么字段排序?
字段名
13.最終要展現(xiàn)什么字段?
字段+聚合函數(shù)
Selecta.S#from SC a, SC bwherea.sno=b.sno and a.C#=’001’ and b.C#=’002’
14.是否去重?
distinct