- 在线时间
- 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可用)
5 r6 M" b2 O: }/ k法一:select * into b from a where 1<>1(仅用于SQlServer)7 b4 o4 I1 b- L5 ?5 u! t; L' l
法二:select top 0 * into b from a6 f/ T; ?( [* a1 Y4 ? i# c3 T
2、说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用)3 I* G% Y5 A- f# N4 L9 E5 w% o
insert into b(a, b, c) select d,e,f from b;
9 R9 f0 g2 } C2 y' g7 A
$ }. j7 i7 ^/ K/ Y/ {# o3、说明:跨数据库之间表的拷贝(具体数据使用绝对路径) (Access可用)8 h/ g( y" k) `0 T/ Y2 H) S
insert into b(a, b, c) select d,e,f from b in ‘具体数据库’ where 条件
' Y' J/ C4 Q' O3 `6 h例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..4 v. {' M G! V9 S% d G3 k" N
% U5 G Q6 ^/ t3 ^) W. W4、说明:子查询(表名1:a 表名2:b)
8 I" I0 { l* H' uselect a,b,c from a where a IN (select d from b ) 或者: select a,b,c from a where a IN (1,2,3)" G% t% @" |0 b) G$ Z4 [# e' ?
3 H1 [9 x) D5 q. w5 z7 d& b5 Z5、说明:显示文章、提交人和最后回复时间
6 t: c) Z# P5 B* nselect a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b
4 T L6 y* \0 V1 z# B4 K
" h: w3 ~" e3 A! M5 P W3 l6、说明:外连接查询(表名1:a 表名2:b)' o& j0 y# i; R4 l
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c0 c7 @6 r& H2 _5 Q" E7 Z5 {
G6 E' G7 G- X6 N6 @
7、说明:在线视图查询(表名1:a )/ d6 B/ {* s1 u6 X% _; \
select * from (SELECT a,b,c FROM a) T where t.a > 1;
( h1 _& S8 N3 r$ O- a8 J0 m
0 u+ I5 S) R: J4 ~1 N8、说明:between的用法,between限制查询数据范围时包括了边界值,not between不包括4 c* c4 O/ h, W) a
select * from table1 where time between time1 and time2- }* K" V' o- z$ X' q
select a,b,c, from table1 where a not between 数值1 and 数值23 l" x4 T! f+ \6 N
# L/ s G2 m( }$ ^% k
9、说明:in 的使用方法
3 `, k1 m. m( r" b+ X2 D( q0 Cselect * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)
n+ ^" B; o/ m% M( L! ~- F5 Q" e2 n4 {4 P
10、说明:两张关联表,删除主表中已经在副表中没有的信息* V( V$ X7 g2 Y7 z
delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )
% D E; p# j* s
5 d% Z8 e1 N8 h! V5 J8 M11、说明:四表联查问题:& C' ]7 H# B) T0 J& N, G7 [" y6 _
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 ....., G5 e! r& {9 z5 o, W
% V* n$ c0 I5 W
12、说明:日程安排提前五分钟提醒4 `* Y& j" m. ]$ V7 p/ V5 h2 [
SQL: select * from 日程安排 where datediff('minute',f开始时间,getdate())>5( F- ]1 L' @: u0 C$ I
9 f3 R8 R a1 U7 w& {13、说明:一条sql 语句搞定数据库分页" F, ` Y# O% [3 S; K+ ]/ r
select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段6 s4 R9 S& W1 a
具体实现:& @2 X) ~. D7 l" ?
关于数据库分页:! n9 s, {7 e; m* ~6 I. F
) [+ d. Q3 T3 w* ?, v" w
declare @start int,@end int
- T; t5 _! u0 @9 h7 p8 v4 F( L& ?& u
@sql nvarchar(600)" A) t2 X/ w+ \6 T( s
+ u8 i7 @* u0 \! W2 [ 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)’
% p4 [% Y! R; G6 U- [' S8 i8 |( C7 N+ u
exec sp_executesql @sql
1 j! w( g( X5 T% w$ v% i7 {$ I; E) R/ t" A0 o- V8 x
3 f6 a. T* A# K3 q( p注意:在top后不能直接跟一个变量,所以在实际应用中只有这样的进行特殊的处理。Rid为一个标识列,如果top后还有具体的字段,这样做是非常有好处的。因为这样可以避免 top的字段如果是逻辑索引的,查询的结果后实际表中的不一致(逻辑索引中的数据有可能和数据表中的不一致,而查询时如果处在索引则首先查询索引)' M/ {6 L. p8 }% {' g5 F2 |
. f7 n o+ d6 _
14、说明:前10条记录
, j3 \& T- V$ P5 ^( d! w/ {, s) D: Aselect top 10 * form table1 where 范围
5 ~. }0 @: c1 Z% y) x1 H
4 U' j/ ~0 R! g, X, Z$ i15、说明:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(类似这样的用法可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)
: V, ~, R ~% C0 wselect a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)
& i: j" u3 x T! W0 U- n& `. l' r# x/ j. |$ U. T
16、说明:包括所有在 TableA 中但不在 TableB和TableC 中的行并消除所有重复行而派生出一个结果表$ a) g5 c1 s: B! s
(select a from tableA ) except (select a from tableB) except (select a from tableC)6 p( g7 \+ @) O& A7 h% M
5 S0 G( I# ?. ?& g |; k
17、说明:随机取出10条数据 n' i$ C2 Q# K
select top 10 * from tablename order by newid()
5 {# H: D) u9 X
# C! h' _4 [$ t% s `1 P18、说明:随机选择记录- i. A5 w5 C% E/ A) w5 }
select newid()
* B- ]" \. x) S" U* n( }$ X6 Y8 E6 _% ^+ p. r$ y
来源:codesd.com |
|