sql语句问题,搜索前5000条数据再过滤

来源:百度知道 编辑:UC知道 时间:2024/05/24 19:04:18
数据库tab1,数据分别是

id name read
1 立三 0
2 看透 1
3 离散 1
4 的确 0
5 屗几 1
6 长辈 0
.
.
.
.
.
10000 把柄 1

上表结构,tab1里有10000条数据,我想搜索出前5000条数据,但在前5000条数据里,只输出read值为1的数据.....

希望各位大侠指教指教

select * from (select top 5000 * from table) where read=1

不知道你是什么数据库,所以就给你写了三种方式:
SqlServer:
select top 5000 * from tab1 where read=1
Oracle:
select * from tab1 where read=1 and rownum<=5000;
MySql:
select * from tab1 where read=1 limit 5000;
在access中和在SqlServer中是一样的,如果你的id是连续的话统统都可以这样写:
select * from tab1 where id<=5000 and read=1
以上,希望对你有所帮助!

oracle
select * from tab1 t where t.id in (
select rownum tab1.id from tab1 order by tab1.id where "Row" < 5000 )
and t.read = 1

sql server
select * from tab1 t where t.id in (
select top(5000) tab1.id from tab1 order by tab1.id )
and t.read = 1

对你表达的前5000条数据有两种理解
(1)符合read=1的前5000条记录
select top 5000 * from TableName
where read=1
(2)id<5000且read=1
select * from TableName
where read=1 and id<50001

select top 5000(read),* from tab1 where read=1 如果你想正序排列,那么就这样就可以,如果倒序的5000那就 加上 gr