SQL基础语句(提升)

1、复制表(只复制结构,源表名:a 新表名:b)

select * into b from a where 1<>1

2、拷贝表

insert into b(a,b,c) select d,e,f from b;

3、跨数据库之间表的拷贝(具体数据使用绝对路径)

insert into b(a,b,c) select d,e,f from b in '数据库路径' 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,username,b.adddate from table a,(select max(adddate)

adddate from table where table.title=a.title)b

6、两张表关联,删除在表2中有表1没有的数据

delete from table1 where not exists(select * from table2 where table1.id=table2.id)

7、日程安排提前五分钟提醒

select * from 日程安排 where datediff('minute',开始时间,getdate())>=5

8、数据库分页

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(500)

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

9、前十条记录

select top 10 * from table1 where 范围

 

posted @ 2016-01-04 15:03  kasauce  阅读(204)  评论(0编辑  收藏  举报