- 在线时间
- 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可用)9 C4 M9 T: t& V/ @# f% ^
法一:select * into b from a where 1<>1(仅用于SQlServer)4 e: g% \ I; j* N
法二:select top 0 * into b from a$ O5 J7 O4 E; W n# X
2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)2 B a; |3 k: R& @+ l
insert into b(a, b, c) select d,e,f from b;
/ l. q: U$ {+ |! E; C" G
7 ~5 {8 q, E4 Q3 [9 X0 F9 _/ u3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)
/ S# {# t# K& T% [; C- einsert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件% k3 |6 K- P6 X# u$ Y
例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..
! p+ u: \8 m- z8 I: ~& a5 q5 m3 I' ?' m( p& r9 s4 R1 E
4、说明:子查询(表名1:a 表名2:b)" ]: q5 S4 h0 S7 b$ m9 Q+ \& C
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)
[8 `; h" e; `& _# i f0 v c. C- p1 G) z" P5 W( z5 L2 s
5、说明:显示文章、提交人和最后回复时间
. ?6 o$ k1 |' U- `$ ]select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
O$ s. u9 i, A- W1 J% G& S! c* q9 F% m6 v0 W- {) w" F0 h7 b
6、说明:外连接查询(表名1:a 表名2:b)
0 X3 z4 L( U- C. [) Mselect a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c; R% l4 e) {$ @
/ o- ^- j* p: G/ _ x8 _7、说明:在线视图查询(表名1:a )
6 q3 M8 }( a& }8 b* ^2 f. S, X9 Kselect * from (SELECT a,b,c FROM a) T where t.a > 1;
3 i( C3 r* {9 p! F$ {5 ~8 V* h# j( f9 T
8、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括0 Z( |2 Y( g) G9 l3 U
select * from table1 where time between time1 and time2
3 I* h+ W" r% u4 E0 Rselect a,b,c, from table1 where a not between 数值1 and 数值2
8 i, M! S1 u# c8 B+ g5 l3 K8 A1 }, v
9、说明:in 的使用方法
# y. N8 B( C7 h) ]6 Kselect * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’): k* Q9 B N* ^/ R
: m6 V4 \# f/ M
10、说明:两张关联表,删除主表中已经在副表中没有的信息
% O& } q$ Z$ n: }6 tdelete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )
6 F! L8 I$ l9 _, h4 i. u* ~$ G z" c, l% x8 w2 o$ p0 p
11、说明:四表联查问题:
4 `1 r' W( p! P& K+ |/ bselect * 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 .....
* q7 H4 `- y# }+ U, ]
; \ }% t' i( I) |) H. s12、说明:日程安排提前五分钟提醒/ z8 L4 D ?# |' [+ c* u! M
SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5
7 V( }& H3 t- P+ ^. s
, u: T; U1 c: z) e) a13、说明:一条sql 语句搞定数据库分页! X! }1 _6 W- n- }; E- I
select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段5 Q0 P. u9 G+ |) ?
具体实现:
1 q6 w b3 { R( C. P关于数据库分页:7 i( K" `6 d$ _
) O) ?0 T# ?9 ^! s" g6 X declare @start int,@end int
6 u& A7 _* @! p8 _' U( n Z! Y) D# ]
@sql nvarchar(600)
: |- \, Y: B* G4 q7 C4 s$ s
7 f/ D6 y2 u# V5 P4 ] 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)’
! Y8 v8 l. W$ G9 ]& V6 t2 i" m6 N8 r% {7 B1 X8 s
exec sp_executesql @sql
' k/ N, F/ m: S4 S) W- m9 M4 E! u6 z" Z' I2 w
/ @3 [* }" B, L/ ~) U( {# b7 X
注意:在top后不能直接跟一个变量,所以在实际应用中只有这样的进行特殊的处理。Rid为一个标识列,如果top后还有具体的字段,这样做是非常有好处的。因为这样可以避免 top的字段如果是逻辑索引的,查询的结果后实际表中的不一致(逻辑索引中的数据有可能和数据表中的不一致,而查询时如果处在索引则首先查询索引)
6 k$ C3 M6 ~1 b+ X& Z0 n4 \( h& g T& P! l2 m
14、说明:前10条记录
* z7 x8 X1 h+ W# v9 e" l9 Qselect top 10 * form table1 where 范围9 B6 P( ~% L$ v0 G
1 a& U0 C: z' Y! g# g15、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)
, t, B' V% x6 `: W) Pselect a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)
8 d* k0 Y/ h. R* r3 N
$ Z8 c( j& N; K* g( y16、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表& X+ } ^+ B1 f' s, ?( {
(select a from tableA ) except (select a from tableB) except (select a from tableC)
0 K5 n, j6 W; ~! Q
?& }" g8 y4 y0 E9 P/ e4 {17、说明:随机取出10条数据
) ?! Q& J) ?) `6 d9 i( z- lselect top 10 * from tablename order by newid()
4 h5 U2 t) m" A3 C7 e
; M( w* |, t1 I) {8 U, g18、说明:随机选择记录
9 P+ u# |$ B% ]/ A$ Q8 ?select newid()
2 q8 ]0 D. q1 n# }, N4 L
& T; B% d6 q- Q$ x, v0 a来源:codesd.com |
|