说明字段经典SQL语句大全(二)

PS:今天上午,非常郁闷,有很多简单基础的题问搞得我有些迷茫,哎,代码几天不写就忘。目前又不当COO,还是得用心记代码哦!

    二、晋升

1、说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)
法一:select * into b from a where 1<>1(仅用于SQlServer)
法二:select top 0 * into b from a
2、说明:贝拷表(贝拷据数,源表名:a 标目表名:b) (Access可用)
insert into b(a, b, c) select d,e,f from b;

3、说明:跨据数库之间表的贝拷(体具据数应用绝对路径) (Access可用)
insert into b(a, b, c) select d,e,f from b in ‘体具据数库’ where 条件
例子:..from b in '"&Server.MapPath(".")&"\data.mdb" &"' where..

4、说明:子查询(表名1:a 表名2: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)

5、说明:表现文章、提交人和最后复回时光
select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b

6、说明:外连接查询(表名1:a 表名2:b)
select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c

7、说明:线在视图查询(表名1:a )
select * from (SELECT a,b,c FROM a) T where t.a > 1;

8、说明:between的用法,between制约查询据数范围时包含了边界值,not between不包含
select * from table1 where time between time1 and time2
select a,b,c, from table1 where a not between 数值1 and 数值2

9、说明:in 的应用方法
select * from table1 where a [not] in (‘值1’,’值2’,’值4’,’值6’)

10、说明:两张关联表,删除主表中已经在副表中没有的息信
delete from table1 where not exists ( select * from table2 where table1.field1=table2.field1 )

11、说明:四表联查题问:
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 .....

12、说明:日程安排前提五分钟醒提
SQL: select * from 日程安排 where datediff('minute',f开始时光,getdate())>5

13、说明:一条sql 语句搞定据数库页分
select top 10 b.* from (select top 20 主键字段,排序字段 from 表名 order by 排序字段 desc) a,表名 b where b.主键字段 = a.主键字段 order by a.排序字段
体具实现:
关于据数库页分:

declare @start int,@end int

@sql nvarchar(600)

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)’

exec sp_executesql @sql


注 意:在top后不能直接跟一个量变,所以在际实应用中只有这样的行进特别的处置。Rid为一个识标列,如果top后还有体具的字段,这样做非是常有利益 的。因为这样可以防止 top的字段如果是逻辑索引的,查询的结果后际实表中的不一致(逻辑索引中的据数有可能和据数表中的不一致,而查询时如果处在索引则首先查询索引)

14、说明:前10条记载
select top 10 * form table1 where 范围

15、说明:择选在每一组b值同相的据数中对应的a最大的记载的全体息信(似类这样的用法可以用于论坛月每排行榜,月每热销品产分析,按科目成绩排名,等等.)
select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)

16、说明:包含全体在 TableA 中但不在 TableB和TableC 中的行并除消全体重复行而派生出一个结果表
(select a from tableA ) except (select a from tableB) except (select a from tableC)

17、说明:随机出取10条据数
select top 10 * from tablename order by newid()

18、说明:随机择选记载
select newid()

19、说明:删除重复记载
1),delete from tablename where id not in (select max(id) from tablename group by col1,col2,...)
2),select distinct * into temp from tablename
delete from tablename
insert into tablename select * from temp
评价: 这类作操连牵量大的据数的挪动,这类做法不适合大容量但据数作操
3),例如:在一个外部表中入导据数,由于某些原因第一次只入导了一部分,但很难断判体具位置,这样只有在下一次全体入导,这样也就生产很多多少重复的字段,怎样删除重复字段

alter table tablename
--加添一个自增列
add column_b int identity(1,1)
delete from tablename where column_b not in(
select max(column_b) from tablename group by column1,column2,...)
alter table tablename drop column column_b

20、说明:列出据数库里全体的表名
select name from sysobjects where type='U' // U代表用户

21、说明:列出内外的全体的列名
select name from syscolumns where id=object_id('TableName')

22、说明:列示type、vender、pcs字段,以type字段排列,case可以方便地实现多重择选,似类select 中的case。
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
表现结果:
type vender pcs
电脑 A 1
电脑 A 1
盘光 B 2
盘光 A 2
手机 B 3
手机 C 3

23、说明:初始化表table1

TRUNCATE TABLE table1

24、说明:择选从10到15的记载
select top 5 * from (select top 15 * from table order by id asc) table_别名 order by id desc

    每日一道理
只有启程,才会到达理想和目的地,只有拼搏,才会获得辉煌的成功,只有播种,才会有收获。只有追求,才会品味堂堂正正的人。

    更多内容来自-伊来网

文章结束给大家分享下程序员的一些笑话语录: 这年头的互联网真是娱乐了中国,网民们从各种各样的“门”里钻来钻去,又有好多“哥”好多“帝”,值得大家品味不已……网络经典语录,关于IT与互联网,经典与您分享!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值