技术只有在交流分享中才能更上一层楼! 复杂的事简单做。 简单的事认真做。 认真的事重复做。 重复的事创造性做。

精妙SQL语句大全

上一篇 / 下一篇  2008-08-09 19:13:33 / 个人分类:数据库相关

 SQL: select * into b from a where 1<>151Testing软件测试网#X V2BlM O!|"O8fc
     
uRiw!za0      说明:拷贝表(拷贝数据,源表名:a 目标表名:b)
q z_8V4Oh x5I0     
p+w,[,|Q%SU0      SQL: insert into b(a, b, c) select d,e,f from a;
lv7^'j2` V'g0     51Testing软件测试网'|]b;j F
      说明:显示文章、提交人和最后回复时间
bwdE,S0     51Testing软件测试网gVF ~3kE3ql&P&b|
      SQL: select a.title,a.username,b.adddate from table a,(select max(adddate)
/A$c+f^Q0  adddate from table where table.title=a.title) b
\9Y| Mn8eRy0     51Testing软件测试网+v%{m'\+a2p|-YT:~
      说明:外连接查询(表名1:a 表名2:b)51Testing软件测试网A[{ n@h;p~
     
\O {!C;`lv }o0      SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a =51Testing软件测试网*^XkL-s.BM
  b.c
JaFI2q-}xV&s0     51Testing软件测试网1K T8M.n oGRmT
      说明:日程安排提前五分钟提醒
2rP!eS?8?W|0     51Testing软件测试网k+? U mz
      SQL: select * from 日程安排 where
7Qv F*S Z2] N0  datediff(&rsquo;minute&rsquo;,f开始时间,getdate())>551Testing软件测试网(r!`t$ut?7L!a
     51Testing软件测试网 j jO]1}
      说明:两张关联表,删除主表中已经在副表中没有的信息51Testing软件测试网']C$_*tz#yM2v-D
     
4v*\Uv cF8^:~-H0      SQL:51Testing软件测试网'^l]9cs"T d
     
TkQ9biJBTV0      delete from info where not exists ( select * from infobz where51Testing软件测试网zJ&d,T]yG6p7ZT
  info.infid=infobz.infid )51Testing软件测试网#m$bD:ML h\
     
$~6Zos_)k@dp0      说明:--51Testing软件测试网(`~L ](l"l c(`
     51Testing软件测试网"AK4U#V2s
      SQL:
-_[/UP~ U0     51Testing软件测试网^4m-UrDP[
      SELECT A.NUM, A.NAME, B.UPD_DATE, B.PREV_UPD_DATE
5{4p,g^f$W0     51Testing软件测试网`:`t`J8N2w2j+k
      FROM TABLE1,51Testing软件测试网)dY8Ki aE/p b/X)y
     51Testing软件测试网4sJ:pTs1NDT`
      (SELECT X.NUM, X.UPD_DATE, Y.UPD_DATE PREV_UPD_DATE51Testing软件测试网mN*T(]V'Y3XMx0mR
     51Testing软件测试网4T4au BJg
      FROM (SELECT NUM, UPD_DATE, INBOUND_QTY, STOCK_ONHAND51Testing软件测试网](`,~(s?+Z5V/kO
     51Testing软件测试网\Xc _^F
      FROM TABLE251Testing软件测试网 K&v'Q.v&W p+_D#Z#n
     51Testing软件测试网 G,Oy(c4BI
      WHERE TO_CHAR(UPD_DATE,&rsquo;YYYY/MM&rsquo;) = TO_CHAR(SYSDATE,51Testing软件测试网d| ?b2vC
  &rsquo;YYYY/MM&rsquo;)) X,51Testing软件测试网g8b/q7gw
     
t x/Ks|%?%w-A0      (SELECT NUM, UPD_DATE, STOCK_ONHAND51Testing软件测试网:TF kyB4g-u
     
Q@.gYbI Xr&D0      FROM TABLE251Testing软件测试网*Y1^j{\&g
     51Testing软件测试网f8@(wR8b\*A7Q8Fo
      WHERE TO_CHAR(UPD_DATE,&rsquo;YYYY/MM&rsquo;) =51Testing软件测试网U H'|2MW[7uod
     
5Qz(T*O2H^N-hC0QN0      TO_CHAR(TO_DATE(TO_CHAR(SYSDATE, &rsquo;YYYY/MM&rsquo;) ||51Testing软件测试网 q HYiV(GM/_G
  &rsquo;/01&rsquo;,&rsquo;YYYY/MM/DD&rsquo;) - 1, &rsquo;YYYY/MM&rsquo;) ) Y,
wk:Yu"@_ K4D0     
e9d!zOajP0      WHERE X.NUM = Y.NUM (+)
;DD\ xbSo ?Gb0     51Testing软件测试网im;{,P u9D)C ~
      AND X.INBOUND_QTY + NVL(Y.STOCK_ONHAND,0) <> X.STOCK_ONHAND ) B
0A8N*oLL0     51Testing软件测试网:S0b$c;|VOD
      WHERE A.NUM = B.NUM
0bp|4u| |Z:V ]zP0     51Testing软件测试网 be BK+O.a$d Dj
      说明:--
Y2H|hyX~$\0     51Testing软件测试网[]x$o0` WU+J]
      SQL:
m*\$kE/D+LQ0     51Testing软件测试网e:q@B*EO
      select * from studentinfo where not exists(select * from student where51Testing软件测试网 E,s0ii6J^nE3De
  studentinfo.id=student.id) and
];RfMXXY4g'aS0  系名称=&rsquo;&quot;&strdepartmentname&&quot;&rsquo; and51Testing软件测试网v r/V {X&A2WWL
  专业名称=&rsquo;&quot;&strprofessionname&&quot;&rsquo; order by 性别,生源地,高考总成绩51Testing软件测试网7Z+\*Cv0{ zX
     
U8o UMgc5I^w0      说明:51Testing软件测试网0lRd&f`d[y
     51Testing软件测试网hF Y]]4w#vhD%{I
      从数据库中去一年的各单位电话费统计(电话费定额贺电化肥清单两个表来源)51Testing软件测试网#xJ s5aK4sL"T'e
     51Testing软件测试网c|Ua p4VU#Mw
      SQL:
{SMH-[kzn0     
o oQR!l9Y,~W0      SELECT a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate,51Testing软件测试网v8vyZh"j
  &rsquo;yyyy&rsquo;) AS telyear,51Testing软件测试网4sy7yHHr?M
     51Testing软件测试网)DdH&[I B9Z
      SUM(decode(TO_CHAR(a.telfeedate, &rsquo;mm&rsquo;), &rsquo;01&rsquo;,51Testing软件测试网E-Q0o|uvMY
  a.factration)) AS JAN,51Testing软件测试网r"|m:Vbr8c N
     
'c uZlV0      SUM(decode(TO_CHAR(a.telfeedate, &rsquo;mm&rsquo;), &rsquo;02&rsquo;,
6Y8w3s.{};N#e0  a.factration)) AS FRI,51Testing软件测试网 zxsO(J ^
     
%q:ng%zHS v0      SUM(decode(TO_CHAR(a.telfeedate, &rsquo;mm&rsquo;), &rsquo;03&rsquo;,51Testing软件测试网/yiVL6Lsr
  a.factration)) AS MAR,51Testing软件测试网$g/\ Lj!x?N*k
     
UM[;u i2g0      SUM(decode(TO_CHAR(a.telfeedate, &rsquo;mm&rsquo;), &rsquo;04&rsquo;,
;w ]t[k0  a.factration)) AS APR,
pY B9A9_0     51Testing软件测试网6epY'x},][ Lt%_
      SUM(decode(TO_CHAR(a.telfeedate, &rsquo;mm&rsquo;), &rsquo;05&rsquo;,51Testing软件测试网&Fi+A&_%ly+I+B
  a.factration)) AS MAY,51Testing软件测试网*w*f5\E:z
     
O$G+DC/C\v0      SUM(decode(TO_CHAR(a.telfeedate, &rsquo;mm&rsquo;), &rsquo;06&rsquo;,
Q |h!\0F0  a.factration)) AS JUE,
#I7J"K4ANq?W0     
8^C;`LL'h |0      SUM(decode(TO_CHAR(a.telfeedate, &rsquo;mm&rsquo;), &rsquo;07&rsquo;,51Testing软件测试网 td"VK]P/Vh gB
  a.factration)) AS JUL,
n8Cg,I ti0     
A$CF%P.D jG?0      SUM(decode(TO_CHAR(a.telfeedate, &rsquo;mm&rsquo;), &rsquo;08&rsquo;,51Testing软件测试网bn A c]5^n P
  a.factration)) AS AGU,51Testing软件测试网dky3T;Q N
     
.o3M0K|r&E0      SUM(decode(TO_CHAR(a.telfeedate, &rsquo;mm&rsquo;), &rsquo;09&rsquo;,
5G%|-w aiJ/H0  a.factration)) AS SEP,
1g&{ ~$B{],h'c0     51Testing软件测试网u;D}-l3Qt
      SUM(decode(TO_CHAR(a.telfeedate, &rsquo;mm&rsquo;), &rsquo;10&rsquo;,
h+p|.AH0  a.factration)) AS OCT,51Testing软件测试网X+_o5Lzc
     
*Ec7I^k$du0A0      SUM(decode(TO_CHAR(a.telfeedate, &rsquo;mm&rsquo;), &rsquo;11&rsquo;,51Testing软件测试网#E0Z3~-v~8UZA,V
  a.factration)) AS NOV,
E%a7s!|:C(L.u K0     
;ltMofyz0      SUM(decode(TO_CHAR(a.telfeedate, &rsquo;mm&rsquo;), &rsquo;12&rsquo;,
!wZ;^({ `0  a.factration)) AS DEC51Testing软件测试网f3t Cf@.^MR
     51Testing软件测试网)z/{/I XC/x.V
      FROM (SELECT a.userper, a.tel, a.standfee, b.telfeedate, b.factration51Testing软件测试网7T1p z ]h,e Seq!g
     51Testing软件测试网#L j;X bJ.m
      FROM TELFEESTAND a, TELFEE b
U"\J.l9s9@%AP#Y0     
5P XStug W0      WHERE a.tel = b.telfax) a51Testing软件测试网1KU2[x-Z
     51Testing软件测试网?%o:^/h6p7Lh
      GROUP BY a.userper, a.tel, a.standfee, TO_CHAR(a.telfeedate,51Testing软件测试网u2X{L3aX$h
  &rsquo;yyyy&rsquo;)51Testing软件测试网-ns JA'D GWV!E#g
     
a ^2R6os@$Y0      说明:四表联查问题:
q)Jn\(j)U0     
Z1[HE ?I2p}#sQu0      SQL: select * from a left inner join b on a.a=b.b right inner join c on51Testing软件测试网pY0NlXb*`x u
  a.a=c.c inner join d on a.a=d.d where .....
b-k z(DJ0     51Testing软件测试网S7A\ k!\DPd8gn
      说明:得到表中最小的未使用的ID号51Testing软件测试网'd1}yN!^
     
I,F4YH9Lg0      SQL:51Testing软件测试网+B%d2b0l]Pz
     51Testing软件测试网 @QpfqX0@
      SELECT (CASE WHEN EXISTS(SELECT * FROM Handle b WHERE b.HandleID = 1) THEN51Testing软件测试网8q{-\b.?wu
  MIN(HandleID) + 1 ELSE 1 END) as HandleID
I(E[ vKo0     
m-}w;Q,PrGBd0      FROM Handle
@qyk1d OD0     
0H:k"F.L.f6E0      WHERE NOT HandleID IN (SELECT a.HandleID - 1 FROM Handle a)

TAG: 数据库相关

 

评分:0

我来说两句

日历

« 2024-05-08  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 22132
  • 日志数: 36
  • 文件数: 1
  • 建立时间: 2007-11-13
  • 更新时间: 2008-09-21

RSS订阅

Open Toolbar