- 在线时间
- 0 小时
- 最后登录
- 2011-11-24
- 注册时间
- 2011-11-23
- 听众数
- 0
- 收听数
- 0
- 能力
- 0 分
- 体力
- 8 点
- 威望
- 0 点
- 阅读权限
- 10
- 积分
- 3
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 1
- 主题
- 0
- 精华
- 0
- 分享
- 0
- 好友
- 0
升级   60% 该用户从未签到
|
1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用): E/ m* l o% q' P; n6 d d, R. e
法一:select * into b from a where 1<>1(仅用于SQlServer)
& g) S$ ]1 H: T. i ?* ?- G; W# a+ {法二:select top 0 * into b from a
! m$ C7 a( w* w2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)
2 p0 _. E. U2 I0 hinsert into b(a, b, c) select d,e,f from b;6 J( j: K/ B5 o a2 V9 V
$ ~# g4 l6 ]1 v$ g7 A* j0 E, |
3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)
/ O( T7 I! W# l0 I- xinsert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件6 F* T8 [ \6 _/ b
例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..- c2 ~- c% }! D7 e& T/ G
) ]) ?7 Q1 t# r4 I4、说明:子查询(表名1:a 表名2:b)
- \5 I* ^! l8 X& g. wselect a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3)
7 H& J+ G$ ~" T4 X0 C
: z6 S" W# t$ n" X* U5、说明:显示文章、提交人和最后回复时间
1 ` s& C6 x, I# u. }select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
( `. N/ a! p: O* b0 b- q. ~6 V4 M( g1 E: k0 v% o$ Q
6、说明:外连接查询(表名1:a 表名2:b)/ ]1 E* f) H( ?" i, N% K$ i
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
$ F( H2 R) o' i6 F* l
: H7 |5 ~% Q) }7、说明:在线视图查询(表名1:a )8 l# k3 D' {2 A1 A
select * from (SELECT a,b,c FROM a) T where t.a > 1;
, Y2 f2 @- r3 u7 l# s6 G
: K; x/ S0 O* M4 Y8、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括+ L# |3 L, W0 k3 p% w
select * from table1 where time between time1 and time2! w7 J. h9 K d. ], ~* ^- q
select a,b,c, from table1 where a not between 数值1 and 数值2" c9 F; o9 n- h8 F. L3 N. `
: U g" @4 a1 u8 Q9 j$ Q. ~9、说明:in 的使用方法
9 i$ A8 y5 R h; kselect * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)
1 g3 S2 D8 G) S, {0 E% @
" ?6 p$ t3 e! m4 v* w$ I) r7 J10、说明:两张关联表,删除主表中已经在副表中没有的信息
: y. t8 w5 G% q. D/ p/ f# T3 tdelete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 ); @- h( ]2 u' w( v
" ]* C" Z. w; p/ t11、说明:四表联查问题:1 |9 t8 `5 ~5 Z8 n6 A5 F
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 .....1 g5 J7 c; T2 E1 e
8 A) t! r% _' Q4 F3 `) t* ], ?
12、说明:日程安排提前五分钟提醒9 p* m# m. d# _ u
SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5
; b. J9 c% e7 ]+ n2 G3 P
: O# J9 H8 h+ \0 P N# n# c13、说明:一条sql 语句搞定数据库分页. Q2 C( ]) h U) h! m4 z
select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段7 s7 J) G/ f) V) J, f
具体实现:
+ Y- X Z6 _- ` H$ ~, E关于数据库分页:9 P0 h% p' g; n. Z% p
( ?' w, ^! U: B, B6 i3 ~
declare @start int,@end int% M+ @( q: a4 Y7 [0 L
0 B4 `4 ]' k" j/ n* Y
@sql nvarchar(600)
1 E9 ]- }- w0 o/ d8 z/ J2 l+ P* w' {' Y, g
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)’4 {9 |% k9 Q; Z8 w3 n- l
7 i* h9 n6 a* q i, \
exec sp_executesql @sql
/ ?) u0 t* o) @+ X8 X T+ o6 s. M- J: H
( ]4 l% g2 S6 c% A注意:在top后不能直接跟一个变量,所以在实际应用中只有这样的进行特殊的处理。Rid为一个标识列,如果top后还有具体的字段,这样做是非常有好处的。因为这样可以避免 top的字段如果是逻辑索引的,查询的结果后实际表中的不一致(逻辑索引中的数据有可能和数据表中的不一致,而查询时如果处在索引则首先查询索引)6 n. @8 Q2 D) Z4 W3 t+ S
& k2 D; d5 l4 f' F
14、说明:前10条记录
5 e; j, t x' M. u$ Nselect top 10 * form table1 where 范围
6 m( Y6 q9 }- Y- ~$ b5 ~7 S- V
9 p! a6 j+ U, H3 E/ {15、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)
5 k" K6 u% X3 I5 y) t2 E9 W7 o+ ~select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b). A$ a( b0 D$ {1 f
& {; ?" T: p8 D( E& c) m' d* t
16、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表/ L3 P, C+ K8 l( Y2 N2 }" i& a
(select a from tableA ) except (select a from tableB) except (select a from tableC), K- Q1 \! w# N
, T: f7 d" ?1 L- F* @: `
17、说明:随机取出10条数据
3 P6 E5 s, w; N$ o: E- ^# |select top 10 * from tablename order by newid()
; B. | k+ j; v
O- x/ ` ]8 F h V3 p6 ^18、说明:随机选择记录
' J( C1 E! b* u2 L/ ^select newid()7 T) Y& N+ | H9 ^$ j( o5 B
. p8 {" k7 O6 c
来源:codesd.com |
|