a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 124|回复: 1

[数据库] 2012年计算机三级数据库技术SQL语句辅导:提升篇(2)

[复制链接]
发表于 2012-7-31 21:12:12 | 显示全部楼层 |阅读模式
16、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表   (select a from tableA ) except (select a from tableB) except (select a from tableC)
3 R' J  u0 E; v0 c8 _  17、说明:随机取出10条数据
8 @, |" R- f" P# Y3 U3 }% M& b  select top 10 * from tablename order by newid()
7 j: ~) f4 c( u1 g' g/ h; w  18、说明:随机选择记录. q: S( K9 j0 l' j' i1 _, D7 _. p
  select newid()! H2 L! F, M, Y! H) L8 p
  19、说明:删除重复记录
1 B, {; s( u# ?. X( v( Q  1),delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)7 k) D0 v/ N$ C9 h
  2),select distinct * into temp from tablename
! `' U! e4 ^% c2 N: O3 C  delete from tablename: w$ w/ @1 Y9 Z6 f! r; Q' O
  insert into tablename select * from temp
! q6 s+ [: F+ P& Q  f  评价: 这种操作牵连大量的数据的移动,这种做法不适合大容量但数据操作; I& `. V) N4 M5 ~
  3),例如:在一个外部表中导入数据,由于某些原因第一次只导入了一部分,但很难判断具体位置,这样只有在下一次全部导入,这样也就产生好多重复的字段,怎样删除重复字段2 w8 C1 r) C# {- g% X2 B2 e2 ?
  alter table tablename2 k  \+ a2 c. K/ U
  --添加一个自增列
% [# I, R0 T; y5 l* O0 D  add column_b int identity(1,1)
. i" Y" G: R0 n: {  delete from tablename where column_b not in(+ V" ^* \! \/ F) J
  select max(column_b) from tablename group by column1,column2,...)
) K1 ^2 h7 ]6 Y9 |" S  V9 Y/ A, C% c  alter table tablename drop column column_b
* b& v; r& I; F+ Z; f2 F4 ?  20、说明:列出数据库里所有的表名7 ~4 G7 r- ?  o! {( i) E

: M0 }, U  ^/ K8 L8 i, |% h$ W, ^  select name from sysobjects where type='U' // U代表用户
回复

使用道具 举报

 楼主| 发表于 2012-7-31 21:12:13 | 显示全部楼层

2012年计算机三级数据库技术SQL语句辅导:提升篇(2)

</p>  21、说明:列出表里的所有的列名
- n$ K1 [  C* t$ F+ Y! X( A) h8 r  select name from syscolumns where id=object_id('TableName')$ B' e- [( a2 w7 X$ A& z+ E
  22、说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重选择,类似select 中的case。
( O) n( t& Q" L$ X! m/ s8 o0 Y! N3 B  select type,sum(case vender when 'A' then pcs else 0 end),sum(case vender when 'C' then pcs else 0 end),sum(case vender when 'B' then pcs else 0 end) FROM tablename group by type
2 s. f2 O6 I( c  显示结果:+ r. g: g" q/ _  P" P9 k
  type vender pcs2 d' |7 D2 O2 [) I' U# n  H
  电脑 A 1
( V, O5 h, U- s) Y* M  电脑 A 1
. T+ V' Q- z9 [5 z4 w0 ?* Y# _  光盘 B 20 \( v$ Y1 a" f
  光盘 A 2
) H( H5 Q, L( _0 x4 \9 k# V  E  手机 B 3
9 C# V" ?& N! G; \# S  手机 C 3* k" \4 D2 C9 _' @
  23、说明:初始化表table1* {) }+ s4 V. Q, ~9 Q4 V
  TRUNCATE TABLE table14 x* v5 m2 I) E6 R
  24、说明:选择从10到15的记录- }5 o0 K5 A4 o# y- I
  select top 5 * from (select top 15 * from table order by id asc) table_别名 order by id desc
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-6-19 04:35 , Processed in 0.263636 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表