--如果id 为主键
select top 3 * from yourtable
where id not in(select top 5 id from yourtable order by id)
order by id
select top 3 * from yourtable
where id not in(select top 5 id from yourtable order by id)
order by id
解决方案 »
- "126.12"如何转换成含有两位小数的浮点数?
- 正确加100分奖励!运行的结果应该是R3=11(有三个相同数的记录为11)请问如何修改?
- 字符处理问题,在线等待
- sql server 2000 的数据仓库的例子fordmart2000中不能浏览数据?
- 两个表a,b,一对多的关系,一个sql语句查出以下要求的数据:
- MSSQL Server 2000问题
- 请问高手,如何在MS SQL Server中把数据库A中表名符合某一条件的表导到数据库B中去?
- sql server2000的存储过程中,如何判断一个日期变量的值未今天??
- sql+vb一个小麻烦,给50分?
- sql的书本推荐
- 有不同版本的SQL server的问题,新手不要进
- 推荐基本学习sql语言的好书。。。
select top 3 * from where id in(
select top 8 id from yourtable order by id)
order by id desc
(select top 8 * from YouTable order by col asc) as a
order by col desc
--col 为随意字段
select top 8 id from yourtable order by id)
order by id desc
如果这时用select top 是不是效率会比较低?
用游标是不是效率高一些?
---那就加个自动编号:
select id=identity(int,1,1),* into #a from yourtable order by id
--然后就可以查询任意二个区间的数据了
select * from #a where id>=20000 and id<=20004
那么就会取到第3到第7条数据, 这就不对了. 嘿嘿!
--我的第一个语句可以:测试:
create table #a(id int,name varchar(10))
insert #a(id,name)
select 1,'aaa'
union all select 2,'bbb'
union all select 3,'bbb'
union all select 4,'bbb'
union all select 5,'bbb'
union all select 6,'bbb'
select top 3 * from #a
where id not in(select top 5 id from #a order by id)
order by id
drop table #a
--结果:
id name
----------- ----------
6 bbb(所影响的行数为 1 行)
两个identity会产生冲突。再有,被访问的数据库同一时间有多个用户在访问(用做网站数据库服务器),
在多用户的情况下,创建临时表会不会产生冲突?
insert #a
select 'aaa'
union all select 'bbb'
union all select 'ccc'
--你可以不选该自动编号列,选中也行,只要小变一下就行了:
select no=identity(int,1,1),id=cast(id as int),name into #b from #a
select * from #b
--结果:
no id name
----------- ----------- ----------
1 1 aaa
2 2 bbb
3 3 ccc(所影响的行数为 3 行)另:在多用户的情况下,创建临时表会不会产生冲突?
--不会产生冲突!
declare @a table(iid int identity(1,1),dbname varchar(10))
declare @min int,@max int
select @min=0,@max=10
while @min<@max
begin
set @min=@min+1
insert @a select 'CSDN'+convert(char(4),@min)
end
select * from (select top 8 * from @a ) as a where iid not in (select top 3 iid from @a order by iid asc) order by iid asc
select top 4 * from
(select top 8 * from YouTable order by col asc) as a
order by col desc
的写法,微软都推荐使用这个!
select top效率很高,数据量大的时候只会耗用一些定位的时间,比游标效率不知道高哪里去了!
select top 8 * from tablel
where table1_id not in(
select top 4 table1_id from table1
order by querytimes desc
)
order by querytimes desc谢谢大家!!
(select top 8 * from YouTable order by col asc) as a
order by col desc