查询之order by,group by和having的使用 (转)
上一篇 / 下一篇 2009-09-25 19:26:47 / 个人分类:数据库
(1)order by
\k i7gY ^6Oy:n0
Y2Q;Zn3X0 ORDER BY子句的语法为:
;R.N oYh(d%F+zn0
(P%Z%HP X(b2q0SELECT column1, SUM(column2) FROM "list-of-tables"
z F4R!b5_;TG051Testing软件测试网*?,`J\wW(Z~
ORDER BY "column-list" [ASC | DESC];
9xr-EdO*eW0
yi1ae q}0[ ] = optional
'G#c3V orp051Testing软件测试网!p/]@ jMoKX%N
ORDER BY是一个可选的子句,它允许你根据指定要order by的列来以上升或者下降的顺序来显示查询的结果。例如:
W q,QGn3{"~6\0
Q uW#{y!v0ASC = Ascending Order – 这个是缺省的
Z*O^_:z,xxQ051Testing软件测试网|rsr+{d-g_
DESC = Descending Order51Testing软件测试网wUk C4G;L}+`O&l ]T ]
51Testing软件测试网"j*~ r6oUcla `h
下面举个例子:51Testing软件测试网.S*y^/Kgc
51Testing软件测试网d*q Z!N.L`R f
SELECT employee_id, dept, name, age, salary
p2u(wE8D_0
MW8|*J"O$x8gQ$n#x,d0FROM employee_info51Testing软件测试网"k!K9v6w2B-Ex I
51Testing软件测试网o7C[ u$j&N[%s{c
WHERE dept = 'Sales'51Testing软件测试网l4R2Pul4K9~`2F@
51Testing软件测试网W ]M2CF%i\;aO
ORDER BY salary;51Testing软件测试网5ZTb#LA9n ?W3M
51Testing软件测试网'k?R8kLb%H*{O*X5p
这条SQL语句将从employee_info表中列dept等于'Sales'选择employee_id,、dept、 name、 age和 salary,并且根据他们的salary按升序的顺序来列出检索结果。51Testing软件测试网&K5}-q'u Z7Z _){h
51Testing软件测试网4m:GZ$y;k-vABY
如果你想对多列排序的话,那么在列与列之间要加上逗号,比如 :51Testing软件测试网A@(CH0aae
@8ND:GU"_ s i\0SELECT employee_id, dept, name, age, salary51Testing软件测试网X_T6oN%`
51Testing软件测试网s!F:p u3GZ c h_6`
FROM employee_info
MVT d2mL:A&};\V051Testing软件测试网l;O0O8H.wk
WHERE dept = 'Sales'
$D4q2xjL"?`Ft \#V051Testing软件测试网/@L0e_.LO:` x
ORDER BY salary, age DESC;51Testing软件测试网7B.se\j*tx
5Rt[T$p`0(2)group by51Testing软件测试网"iGe |F2Dg+m
51Testing软件测试网5{8t s'w(F r
一、首先讲讲GROUP BY 子句语法:
?Z)]D-I M9W051Testing软件测试网W)Y0un O V8p
SELECT column1, SUM(column2) FROM "list-of-tables"
J"KH)zY1dXh`k0
:U,nXY(EQv0GROUP BY "column-list";51Testing软件测试网$C1B+e2{K;[/teu
51Testing软件测试网G!Uq)N8] ^0@!Z?Q E
这个GROUP BY子句将集中所有的行在一起,它包含了指定列的数据以及允许合计函数来计算一个或者多个列。当然最好解释的方法是给出一个例子啦:51Testing软件测试网/?@9W8w5n^#e @Ah
3u:FJ L3P S$h0假设我们将从employee表中搜索工资最高的列,可以使用以下的SQL语句:51Testing软件测试网7\k;{G(A/oH#v1d
:h yXI^~0SELECT max(salary), dept51Testing软件测试网 wmi}6q'E"nZn
51Testing软件测试网1p-R;}{7L3F
FROM employee51Testing软件测试网|X5c m3B5yV
51Testing软件测试网 KFZ-zfQ-utxI
GROUP BY dept;
,@Q @cX#W$Fd0
)z_.D(vM n n0这条语句将在每一个单独的部门中选择工资最高的工资。结果他们的salary和dept将被返回。
4WBAQl`g'`gCC051Testing软件测试网2@kwZD%N
二、group by 从英文里理解就是分组。必须有“聚合函数”来配合才能使用,使用时至少需要一个分组标志字段。51Testing软件测试网%O8Y8\-U.Vtv
T B5b`-e-}0什么是“聚合函数”?
DI$]_I:_0e,|G-V0 像sum()、count()、avg()等都是“聚合函数”51Testing软件测试网(H1s.@ozF
使用group by 的目的就是要将数据分类汇总。
m)K;j |a0x0}0
,j#_*b"M.l{Q u@ n-W0 一般如:51Testing软件测试网)?%Z.@m&cMIV+Z ZxC
select 单位名称,count(职工id),sum(职工工资) form. [某表]51Testing软件测试网g_(j4ve$?D"A
group by 单位名称
F(t*Hl6CI9JC0 这样的运行结果就是以“单位名称”为分类标志统计各单位的职工人数和工资总额。51Testing软件测试网Z9}];|Yks!}
&XR1U2W(nD"@0 在sql命令格式使用的先后顺序上,group by 先于 order by。51Testing软件测试网 A&X`n `~7?4G
51Testing软件测试网y9e)T~.H)P0{Q3A*]0\
select 命令的标准格式如下:51Testing软件测试网6cx`7_@4_8fZ'w0K!J
_ ~(Q*gp0 SELECT select_list51Testing软件测试网 va }6kO K$S
[ INTO new_table ]
&d(@Rv1p4rXg0 FROM table_source51Testing软件测试网;k6Gb/r*F&Ap
[ WHERE search_condition ]
$|;?2EZ J5@f0 [ GROUP BY group_by_expression ]
Z#H_ ]e N?h0 [ HAVING search_condition ]
H&Cn0m7x"}0 [ ORDER BY order_expression [ ASC | DESC ] ]51Testing软件测试网])l7R&z})a
51Testing软件测试网qA1`?iZ9B
三、理解group by:
g@ i*xa#VA fi0
O!i#g"r G Gyv`0表1:
w}0C8pmq0
GR U.Fz#twG+FK\s0 type
:@ Mx0W8Kw!on0 51Testing软件测试网![0mo*b Tza4j |;}
udate
VW1OZo\0^0
ile`lB F^.w0 mp3
r?$K*Y!]1jPPk0
5Zl%Bpu h-UKi0 2006-3-251Testing软件测试网%Yp jW To?eb
51Testing软件测试网$g7eoBN1qxr
flash
kze3G1h0
`/e&~ls2x+g5TsZ:l.f*~0 2006-1-251Testing软件测试网&df nzM0\t
]t*Viir0 mp351Testing软件测试网,R whf2FS&KuD*i
51Testing软件测试网 Ph0bX(x~]
2006-2-251Testing软件测试网7{4~U/Yu)g4|,zz
g,W,sb)q0在表1中,假设要取出最后更新的某一类型(type)产品的日期51Testing软件测试网f [WW!BLC9B
那么就要使用group by type的方式
%b)]:yjLP{Yv0select type from 表1 group by type
Tc`}.Z:@"P_,]0但这样就无法读到udate
$Uc-o wr^k0 51Testing软件测试网f y!Vg!QX
再来51Testing软件测试网Salf#BgY6r
select type,udate from 表1 group by type51Testing软件测试网F4t&Ic2vMk(UB,j
这种写法是错误的。51Testing软件测试网0a$^8Vs5[f5v*S
原因是type和udate 不是一对一的关系。就是说一种type有几个udate51Testing软件测试网 P9`8m8R4e g
所以51Testing软件测试网&H;CyS.{$Y%Ms\
selct ** group by ** 之后没办法组织形成新的表,不知道要取哪个udate
fF(\ U(U oo%OE6w0 51Testing软件测试网~!jj.G@
再来51Testing软件测试网IL vJr1v6p\
select type,max(udate) from 表1 group by type51Testing软件测试网 Ks%P/S:y5` z3M&kkZ
这样就正确了。
6~X*H$y\ H0取的是最大的udate ,一对一成立。
\k i7gY ^6Oy:n0
Y2Q;Zn3X0 ORDER BY子句的语法为:
;R.N oYh(d%F+zn0
(P%Z%HP X(b2q0SELECT column1, SUM(column2) FROM "list-of-tables"
z F4R!b5_;TG051Testing软件测试网*?,`J\wW(Z~
ORDER BY "column-list" [ASC | DESC];
9xr-EdO*eW0
yi1ae q}0[ ] = optional
'G#c3V orp051Testing软件测试网!p/]@ jMoKX%N
ORDER BY是一个可选的子句,它允许你根据指定要order by的列来以上升或者下降的顺序来显示查询的结果。例如:
W q,QGn3{"~6\0
Q uW#{y!v0ASC = Ascending Order – 这个是缺省的
Z*O^_:z,xxQ051Testing软件测试网|rsr+{d-g_
DESC = Descending Order51Testing软件测试网wUk C4G;L}+`O&l ]T ]
51Testing软件测试网"j*~ r6oUcla `h
下面举个例子:51Testing软件测试网.S*y^/Kgc
51Testing软件测试网d*q Z!N.L`R f
SELECT employee_id, dept, name, age, salary
p2u(wE8D_0
MW8|*J"O$x8gQ$n#x,d0FROM employee_info51Testing软件测试网"k!K9v6w2B-Ex I
51Testing软件测试网o7C[ u$j&N[%s{c
WHERE dept = 'Sales'51Testing软件测试网l4R2Pul4K9~`2F@
51Testing软件测试网W ]M2CF%i\;aO
ORDER BY salary;51Testing软件测试网5ZTb#LA9n ?W3M
51Testing软件测试网'k?R8kLb%H*{O*X5p
这条SQL语句将从employee_info表中列dept等于'Sales'选择employee_id,、dept、 name、 age和 salary,并且根据他们的salary按升序的顺序来列出检索结果。51Testing软件测试网&K5}-q'u Z7Z _){h
51Testing软件测试网4m:GZ$y;k-vABY
如果你想对多列排序的话,那么在列与列之间要加上逗号,比如 :51Testing软件测试网A@(CH0aae
@8ND:GU"_ s i\0SELECT employee_id, dept, name, age, salary51Testing软件测试网X_T6oN%`
51Testing软件测试网s!F:p u3GZ c h_6`
FROM employee_info
MVT d2mL:A&};\V051Testing软件测试网l;O0O8H.wk
WHERE dept = 'Sales'
$D4q2xjL"?`Ft \#V051Testing软件测试网/@L0e_.LO:` x
ORDER BY salary, age DESC;51Testing软件测试网7B.se\j*tx
5Rt[T$p`0(2)group by51Testing软件测试网"iGe |F2Dg+m
51Testing软件测试网5{8t s'w(F r
一、首先讲讲GROUP BY 子句语法:
?Z)]D-I M9W051Testing软件测试网W)Y0un O V8p
SELECT column1, SUM(column2) FROM "list-of-tables"
J"KH)zY1dXh`k0
:U,nXY(EQv0GROUP BY "column-list";51Testing软件测试网$C1B+e2{K;[/teu
51Testing软件测试网G!Uq)N8] ^0@!Z?Q E
这个GROUP BY子句将集中所有的行在一起,它包含了指定列的数据以及允许合计函数来计算一个或者多个列。当然最好解释的方法是给出一个例子啦:51Testing软件测试网/?@9W8w5n^#e @Ah
3u:FJ L3P S$h0假设我们将从employee表中搜索工资最高的列,可以使用以下的SQL语句:51Testing软件测试网7\k;{G(A/oH#v1d
:h yXI^~0SELECT max(salary), dept51Testing软件测试网 wmi}6q'E"nZn
51Testing软件测试网1p-R;}{7L3F
FROM employee51Testing软件测试网|X5c m3B5yV
51Testing软件测试网 KFZ-zfQ-utxI
GROUP BY dept;
,@Q @cX#W$Fd0
)z_.D(vM n n0这条语句将在每一个单独的部门中选择工资最高的工资。结果他们的salary和dept将被返回。
4WBAQl`g'`gCC051Testing软件测试网2@kwZD%N
二、group by 从英文里理解就是分组。必须有“聚合函数”来配合才能使用,使用时至少需要一个分组标志字段。51Testing软件测试网%O8Y8\-U.Vtv
T B5b`-e-}0什么是“聚合函数”?
DI$]_I:_0e,|G-V0 像sum()、count()、avg()等都是“聚合函数”51Testing软件测试网(H1s.@ozF
使用group by 的目的就是要将数据分类汇总。
m)K;j |a0x0}0
,j#_*b"M.l{Q u@ n-W0 一般如:51Testing软件测试网)?%Z.@m&cMIV+Z ZxC
select 单位名称,count(职工id),sum(职工工资) form. [某表]51Testing软件测试网g_(j4ve$?D"A
group by 单位名称
F(t*Hl6CI9JC0 这样的运行结果就是以“单位名称”为分类标志统计各单位的职工人数和工资总额。51Testing软件测试网Z9}];|Yks!}
&XR1U2W(nD"@0 在sql命令格式使用的先后顺序上,group by 先于 order by。51Testing软件测试网 A&X`n `~7?4G
51Testing软件测试网y9e)T~.H)P0{Q3A*]0\
select 命令的标准格式如下:51Testing软件测试网6cx`7_@4_8fZ'w0K!J
_ ~(Q*gp0 SELECT select_list51Testing软件测试网 va }6kO K$S
[ INTO new_table ]
&d(@Rv1p4rXg0 FROM table_source51Testing软件测试网;k6Gb/r*F&Ap
[ WHERE search_condition ]
$|;?2EZ J5@f0 [ GROUP BY group_by_expression ]
Z#H_ ]e N?h0 [ HAVING search_condition ]
H&Cn0m7x"}0 [ ORDER BY order_expression [ ASC | DESC ] ]51Testing软件测试网])l7R&z})a
51Testing软件测试网qA1`?iZ9B
三、理解group by:
g@ i*xa#VA fi0
O!i#g"r G Gyv`0表1:
w}0C8pmq0
GR U.Fz#twG+FK\s0 type
:@ Mx0W8Kw!on0 51Testing软件测试网![0mo*b Tza4j |;}
udate
VW1OZo\0^0
ile`lB F^.w0 mp3
r?$K*Y!]1jPPk0
5Zl%Bpu h-UKi0 2006-3-251Testing软件测试网%Yp jW To?eb
51Testing软件测试网$g7eoBN1qxr
flash
kze3G1h0
`/e&~ls2x+g5TsZ:l.f*~0 2006-1-251Testing软件测试网&df nzM0\t
]t*Viir0 mp351Testing软件测试网,R whf2FS&KuD*i
51Testing软件测试网 Ph0bX(x~]
2006-2-251Testing软件测试网7{4~U/Yu)g4|,zz
g,W,sb)q0在表1中,假设要取出最后更新的某一类型(type)产品的日期51Testing软件测试网f [WW!BLC9B
那么就要使用group by type的方式
%b)]:yjLP{Yv0select type from 表1 group by type
Tc`}.Z:@"P_,]0但这样就无法读到udate
$Uc-o wr^k0 51Testing软件测试网f y!Vg!QX
再来51Testing软件测试网Salf#BgY6r
select type,udate from 表1 group by type51Testing软件测试网F4t&Ic2vMk(UB,j
这种写法是错误的。51Testing软件测试网0a$^8Vs5[f5v*S
原因是type和udate 不是一对一的关系。就是说一种type有几个udate51Testing软件测试网 P9`8m8R4e g
所以51Testing软件测试网&H;CyS.{$Y%Ms\
selct ** group by ** 之后没办法组织形成新的表,不知道要取哪个udate
fF(\ U(U oo%OE6w0 51Testing软件测试网~!jj.G@
再来51Testing软件测试网IL vJr1v6p\
select type,max(udate) from 表1 group by type51Testing软件测试网 Ks%P/S:y5` z3M&kkZ
这样就正确了。
6~X*H$y\ H0取的是最大的udate ,一对一成立。