我们拒绝平庸,拒绝随波逐流,拒绝墨守成规,让梦想不再流浪。

oracle语法 续

上一篇 / 下一篇  2014-05-27 17:29:17 / 个人分类:甲骨文

51Testing软件测试网C5kGR:o

使用表达式51Testing软件测试网K!qzN ?;@\
=====================================================================51Testing软件测试网V[#T#B3hD

}[ @0\u9uf0Select  ename || '   是一位   ' || job As 雇员细节,51Testing软件测试网,ZRh NY2Xi
        to_char(hiredate,'yyyy-mm-dd') As 雇佣时间,51Testing软件测试网$S9T+\*o:F)bW U
        sal*1.251Testing软件测试网gpz)I"I3v
From emp;51Testing软件测试网l?-qt5Y$IY
51Testing软件测试网_:Oa&p![? L|'[U
=====================================================================51Testing软件测试网 c*f!E&V6K Ith&TI{6x
取消重复行 distinct
7R,x]jF5t0=====================================================================
JS8P1i)Y;kr,Hq4F&aYw0
J0X,o5w)H&^0Select Distinct deptno, job From emp;51Testing软件测试网!HJhO4p
51Testing软件测试网fa m!]~z
=====================================================================
k_@o1ysq`D3U!E;@0 指定列排序 order by51Testing软件测试网"G2k4j Vu,I
=====================================================================51Testing软件测试网Y%O.Q1@{A#JA
asc 升序,desc 降序51Testing软件测试网T R_U D
--------------------------------------------
"@:X:K2g*H0Select * From emp51Testing软件测试网a2dE%Na L&HNY{
where sal between 1500 and 3000
G&X7p"m;|8Y+F0Order By deptno Desc,ename;51Testing软件测试网+U%Q7ME(f%h W-X'H;s`
51Testing软件测试网H(f b;XJB7B7O
--------------------------------------------
aHObX@dP4c0如果使用dsitinct,排序列必须是选择列51Testing软件测试网e3yS,x+];spr
---------------------------
4|4siY*z0select distinct depton, job 51Testing软件测试网cpx9?"o!_ G0w v
from emp51Testing软件测试网"Q6f,MC&rkSI
order by job;
Q8h Q.]^IJ|oGRm0---------------------------------------------
)iNy1V-E0order by 子句必须是最后一个子句51Testing软件测试网TY S/i;DS?
============================================================51Testing软件测试网ZQX7T Zg!w
    分组查询语句51Testing软件测试网Ny8Il+V4e,|k
============================================================
9nw(s7i-m*QxL01。组处理函数不能出现在 where 子句中
l,u?!lP02。选择列表中的列、表达式,必须出现在 group by 子句中
${Z9{cT03。组处理函数中可以指定 all 和 distinct
z tD9h3l T0============================================================51Testing软件测试网6u}}:dV-F `
分组函数
#pef.W)xla$cbS#N0---------------------------------------51Testing软件测试网Z/B&c8a#]0u|'fYe
select avg(sal)            as avg1,
(?ds\w4~0       avg(distinct sal)   as avg2,51Testing软件测试网 P!Y7o3v#coR
       max(sal)            as max,
\I$ge4q)LG$l`0       min(sal)            as min,
vp@(Q.KXMb7e"c0       sum(sal)            as sum,51Testing软件测试网"HF ?-W6\)\&[I N$N
       count(*)            as cnt1,51Testing软件测试网 v"X9LZS/^7c'Fx
       count(sal)          as cnt2,
P&[u"K J6U ik5l0       count(distinct sal) as cnt3
%H I_L)A&O{9w0from   emp
CTIEbvN"A0where  deptno = 30;
8W^hnh,[kH6h7?051Testing软件测试网!Y2QC-\c feT,I
============================================================
_Z.bOA#Q%o+d|6o0单列分组
~+fu A]0---------------------------------------------51Testing软件测试网"cw b4n/P!zE
select deptno, avg(sal), max(sal) from emp51Testing软件测试网lI/yg1S\ m$Y
group by deptno;
[A4an;n0
yX)d ]&uc i0select deptno, avg(sal), max(sal) from emp
{+Su_"e0group by deptno51Testing软件测试网,zK \J_$OQ;u1X
order by avg(sal);51Testing软件测试网 ]dF!nY-Ov\

{!o,^A0h0============================================================51Testing软件测试网XG\-N$p
多列分组
,N:k5M'|'}f os W(G0---------------------------------------------51Testing软件测试网7EZ tFv y)rRj
select deptno, job, avg(sal), max(sal), from emp51Testing软件测试网 WJGs p'ZkV
group by deptno, job;51Testing软件测试网y N9R,Kf(^*pl? b

qWbBzchD0============================================================
/tLBNf'j+r0============================================================51Testing软件测试网DQQ(E7t E5U~"N @%K
rollup 用于生成横向统计信息51Testing软件测试网!W)h hVX?k
--------------------------
BV2fJ8V0Select deptno, job, Avg(sal), Max(sal) From emp
)lN y;d)w0Vt;p0Group By rollup(deptno,job)51Testing软件测试网|T{.v7fl?8V
51Testing软件测试网"`'T0T*q9|%Irh5J?
--------------------------------------------------51Testing软件测试网$v1I/`/c3j,rk ^,xMM
cube  用于生产纵向统计信息51Testing软件测试网 M"qBtd [1k
------------------------------
G8}+uX5s2N;C6F0Select deptno, job, Avg(sal), Max(sal) From emp
;t;s+P3P`} e0Group By Cube(deptno,job)
&m'x-ga:G7Y0
aH(G.xS;B,j3D3p0
T"s|#s:oe n0============================================================
'`!d2D/F\'^#H0      having 子句51Testing软件测试网|H X-fy{qU
============================================================
&D"` lV5jUr(Z rc0select deptno, avg(sal), max(sal) from emp51Testing软件测试网J`.i;Mq Z
group by deptno51Testing软件测试网&P;N M/V)}
having avg(sal) > 2000;
;PLE8w$Rr te!q051Testing软件测试网SVGW6@ lg5Z
不等连接51Testing软件测试网 {8BL-\J9U!v5a
----------------------------51Testing软件测试网/U.ZJjcN
Select e.ename, e.sal, s.grade51Testing软件测试网%H"^VA&N5a ak
From emp e,salgrade s
5P*x)^rr i nF0Where e.sal Between s.losal And s.hisal
K)sZ7AG-W8a0And e.deptno = 3051Testing软件测试网8c4R:Qh.\}zk
51Testing软件测试网1@j2l`ELa/l
===========================================================
I?S)ioW0自连接
;N.Tg*{ZlFJ0---------------------------------------
$k|0`u/do3a0Select e.ename As 雇员, p.ename As 管理员51Testing软件测试网'wWn"Hi ^G%W
From emp e,emp p51Testing软件测试网K+[1Z:V%y z:L
Where e.mgr = p.empno
jW&ChQx0And e.deptno = 30
Z ^0ah!BVX0
s [~/uJh7S&{ CS0===========================================================51Testing软件测试网.{ Dn5mz7Y4N?d;_
      合并查询
F.dcwp0===========================================================51Testing软件测试网0Mb0Z:g%{&s ?vRob0P
union --- 两个集合的并集,去掉重复行,按第一列结构排序
OII;z-[s s)R0-------------------------
"ule6i,y^a0Select empno, ename, mgr From emp51Testing软件测试网"N biyKU
Where deptno = 3051Testing软件测试网 i z&Hf"x
Union
ez*?T,Vq.Cbj%i0Select empno, ename, mgr From emp
d{ Y:[}1k){i7je0Where job = 'MANAGER'
zpJ _~9Hy0===========================================================
^Mw |:SA-[-H0union all --- 两个集合并集,不去重复行,不排序51Testing软件测试网6Pz6aln$VA
-------------------------
wg5M"\6E0Select empno, ename, mgr From emp
3D]8Na n?0Where deptno = 3051Testing软件测试网b[8|K)j\Z
Union All
FW!gQJ0Select empno, ename, mgr From emp
S*A u r-A2yfOw$Z0Where job = 'MANAGER'
+TTGF;yl!\;r(k(\4Y0
/W)ko,Fk;Z*z"Nl0===========================================================
f!B+l ?%E1\N+X0Intersect --- 只会显示同时存在两个集合中的数据
B9@A&y I`-E0-----------------------------------51Testing软件测试网 at m#`AJal
Select empno, ename, mgr From emp51Testing软件测试网CB&r,j8O?sg2[
Where deptno = 30
bp1sPn~O{0Intersect
/W"X3A U&y;D0Select empno, ename, mgr From emp
!|W)l,Wly+Ke.V0Where job = 'MANAGER' 51Testing软件测试网%l*h`efk#zR#@

FO.y&ft#AV6k0===========================================================
:_~#h9ff1\9B0minus --- 在一个集合存在,在第二个集合不存在的数据,按第一个排序
M eSV"[0-----------------------------------
] B9U P({'Y5A b%Z@0Select empno, ename, mgr From emp51Testing软件测试网 qr8W7QLu9P
Where deptno = 3051Testing软件测试网&z9?Qp)|1p!n
Minus51Testing软件测试网 oI6JH vAZ$t.tj;w J
Select empno, ename, mgr From emp51Testing软件测试网To Zx/|`r
Where job = 'MANAGER' 51Testing软件测试网!@9r5T(?2a8al
51Testing软件测试网nh-v-apY
===========================================================
{(BVJr5r ^0合并查询中,只能有一个 order by 子句。在这个子句中使用列名或
1xW*FU-d2s'BC0第一个查询的别名。
v2t"W;U1rHL5I k{/C0--------------------------------------
9Insci G0Select empno, ename 雇员, mgr From emp51Testing软件测试网7J9g-f:US a-F
Where deptno = 30
zPOu!hrHn0Minus51Testing软件测试网)_Xd8I ?Y%B
Select empno, ename 雇员, mgr From emp51Testing软件测试网VeX7C%t1Ex
Where job = 'MANAGER' 51Testing软件测试网7y/Vu ER!|H
Order By 雇员51Testing软件测试网pwvP [ b+y)A/f

K:T6S;X9qR-{B0 51Testing软件测试网(Y h@v(r.XW

单行子查询51Testing软件测试网8P eHWZ3n
------------------------------
m;d G T&l^0Select ename, deptno, sal From emp
(c6p:AF\cZJ4V0Where sal = (Select Max(sal) From emp);51Testing软件测试网 H*d0r/p_3`

/r5`1vCI$QKB0============================================================51Testing软件测试网#e,D[HY6@O
多行子查询 --- where子句中使用多行子查询,必须使用多行运算符,51Testing软件测试网:?'b8{\0j
      (in,notin,exists,not exists,all,any)
[;g6U%r)C?0---------------------------------------------------51Testing软件测试网5XH$lE-@7eN
Select ename, deptno, sal, job From emp51Testing软件测试网 x]%N'ax]
Where job In ( Select Distinct job From emp Where deptno = 20 )
Rrn#I:~9GFc q!`y0
| o"_B&Uc_0------------------------------------------------------------51Testing软件测试网 ^:|)@+VI r1Dp9? s
Select e.ename, e.job, e.sal51Testing软件测试网@{dkG^ @(cg
From emp e51Testing软件测试网5H4ZzC)P4I
Where sal > All ( Select sal From emp Where emp.deptno = 20)
)E fM{ h,{0
$?p*H!ohC(O0------------------------------------------------------------51Testing软件测试网*Ef7cV3X7z K
Select e.ename, e.job, e.sal
VGe-nhBU0From emp e51Testing软件测试网"Q)y F8iP3s2fyMe#U
Where sal > Any ( Select sal From emp Where emp.deptno = 20)
8g fR?"yKn`051Testing软件测试网7MQQ;msd:`G4ey
============================================================51Testing软件测试网V i\N_? ^j-b-l
相关子查询51Testing软件测试网{2K!D^p"b
------------------------------51Testing软件测试网"O7T2CFI b.Vl
Select deptno,51Testing软件测试网 |4ejV@n9QI
( Select Max(sal) From emp b Where b.deptno = a.deptno ) maxsal51Testing软件测试网+vD7Q2_O p`l
From emp a
D a |vX$R8P.h0Order By deptno51Testing软件测试网y5@9E+q U$}Z
------------------------------
gmyuH0Wav0Select ename, deptno, sal, job From emp
v(u4A'\2_0Where Exists51Testing软件测试网 Z+C6M`*A,a*v]
(
#YAy x I#K `)Z'j0  Select 'x' From dept51Testing软件测试网9@'_*njP1m
  Where dept.deptno = emp.deptno And dept.loc = 'NEW YORK'51Testing软件测试网ci q'?o1D[Yc
)
H'F5kt:[a051Testing软件测试网F+|tK3Q#X*y Q R
============================================================
4t iI*m%HZ0u0标量子查询 --- 显示每个部门的最高工资员工信息51Testing软件测试网ONT+t/B|
-----------------------51Testing软件测试网(T8I8BU8A/[9N{!L7C(S
select distinct deptno,51Testing软件测试网.t[^So}
(select max(sal) from emp b where b.deptno = a.deptno) maxsal51Testing软件测试网'A ~H0\%@{{;}
from emp a
l YC^ q/xH7j0order by deptno;
n@p;i}}4V,B,}[+_ E051Testing软件测试网)^Rq!orA'A
============================================================
)J;qeDS(}t9O Ly;b0多列子查询 --- 显示与smith部门和岗位完全相同的所有雇员信息。
H$d+V$de%V3A,_0-----------------------
0e-V d]0{y!c d2B0Select ename, deptno, sal, job From emp51Testing软件测试网?-\*o(s(c
Where (deptno, job) =51Testing软件测试网$|!M f?$R,^C9\ms
(Select deptno, job From emp Where ename = 'SMITH')51Testing软件测试网1NK2d&j;ag3s*j
-----------------------
f9~rMi} a R0 显示岗位或者管理员匹配于部门编号为20的所有雇员信息.
Tf@W+O8id a8d0-----------------------
*I4s](W(M9e S4kq5w~0Select ename, deptno, sal, job, mgr From emp51Testing软件测试网-LjIs$h[oEF
Where job In ( Select job From emp Where deptno = 20 )51Testing软件测试网m:[uf7xV"E1N8g|6\
Or    mgr In ( Select mgr From emp Where deptno = 20 )
@$Z(fd k"@*` C)Oy8AG"@0Order By deptno51Testing软件测试网;rH(g fV/lo
51Testing软件测试网 JK6YhaZ.d s3i\
51Testing软件测试网7b)p W@uN

C C$b,`c jp^OD0 51Testing软件测试网-v1JIV k


TAG:

 

评分:0

我来说两句

日历

« 2024-04-30  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 109954
  • 日志数: 89
  • 图片数: 1
  • 文件数: 15
  • 建立时间: 2013-03-01
  • 更新时间: 2018-08-23

RSS订阅

Open Toolbar