a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 127|回复: 1

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

[复制链接]
发表于 2012-7-31 21:12:12 | 显示全部楼层 |阅读模式
 1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)   法一:select * into b from a where 11(仅用于SQlServer)* w# w9 z* v& a: ]
  法二:select top 0 * into b from a& e3 f* h& J1 z3 e- K
  2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)5 a- t4 K3 J# w& W
  insert into b(a, b, c) select d,e,f from b;4 e' L( g) C* v  R( A7 G6 {
  3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)
  T/ Z: H1 B! X$ K) D. d  insert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件, k! R3 R( `5 s" I7 N0 V  Q
  例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..
7 D7 ]* e) a" J+ V$ o  4、说明:子查询(表名1:a 表名2:b): t, v" M1 y+ i/ z; G) d. b
  select a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3)0 a& I* Y, t3 _: n
  5、说明:显示文章、提交人和最后回复时间
" r% C/ u5 B3 |1 |' x! e" p/ r8 }  select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b$ T* ~: U  j, Q( z. D: K
  6、说明:外连接查询(表名1:a 表名2:b)
9 Q1 s8 q; U% p5 E2 E  v; c- z  select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c  O; x$ L" w& q' j" j' r
  7、说明:在线视图查询(表名1:a )
  ]. t, o: L$ J; K# |  ]0 a  select * from (SELECT a,b,c FROM a) T where t.a > 1;$ n! ~+ t! j- X/ w% X+ Q% d2 D2 N- `9 H, r
  8、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括
5 `. v3 v$ V8 D4 i/ s# Z8 K0 J  select * from table1 where time between time1 and time2
/ t6 e  d5 a; G7 K8 e. f
- O1 z4 q6 p* M5 x2 _  select a,b,c, from table1 where a not between 数值1 and 数值2
回复

使用道具 举报

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

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

</p>  9、说明:in 的使用方法9 ^1 F% G) Y# I
  select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)
$ o9 K$ M, b! d! G! m( d0 d  10、说明:两张关联表,删除主表中已经在副表中没有的信息9 ^" V% y  Z7 P  S7 f
  delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )- w7 H7 I3 p# J/ ]/ k. L# M/ V. z
  11、说明:四表联查问题:! Q; U9 D; E% W( B7 u; r
  select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....' A! x! N7 `6 e" L  Y+ R
  12、说明:日程安排提前五分钟提醒
( B4 v8 G: e, A* j  SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5
% }6 Z& g# v7 i3 G) i) Z  13、说明:一条sql 语句搞定数据库分页! B; a. v, I+ Q* R8 A6 P) q" b0 D
  select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段1 V/ }* ~. Z. p5 G
  具体实现:
- c$ V% [3 k# l! `  C0 i  关于数据库分页:
" F7 A8 E+ B' A; [  declare @start int,@end int
1 U7 `( L( w2 U7 i1 O  @sql nvarchar(600)
% c* M8 X% U4 J  set @sql=’select top’+str(@end-@start+1)+’+from T where rid not in(select top’+str(@str-1)+’Rid from T where Rid>-1)’
$ {9 a, o: W. ^: p( J  exec sp_executesql @sql4 h, Q' d+ a1 s: n( @
  注意:在top后不能直接跟一个变量,所以在实际应用中只有这样的进行特殊的处理。Rid为一个标识列,如果top后还有具体的字段,这样做是非常有好处的。因为这样可以避免 top的字段如果是逻辑索引的,查询的结果后实际表中的不一致(逻辑索引中的数据有可能和数据表中的不一致,而查询时如果处在索引则首先查询索引)( E4 ?! p5 _, a  E
  14、说明:前10条记录6 U4 @4 s/ j3 Z2 }* _- E
  select top 10 * form table1 where 范围" a- A' R4 Q( }/ d1 O
  15、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)
2 k* N4 r  g2 i0 T; n5 L$ `  {  select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-5 04:03 , Processed in 0.155807 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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