ASP读取数据

shionco 2008-11-24 07:41:06
select * from a where id>=1 and id<=? and type=1 order by tdate desc

假设表A中有100条数据,我想要查询的数据为50条,且type=1,我写成:
select * from a where id>=1 and id<=50 and type=1 order by tdate desc

这样查询出了 ID为1~50范围内type=1的数据,由于id在1~50范围内有type<>1的数据,所以查询出来的数据不足50条;

如果知道type=1的第50条数据id,假设 id=70,可以写成:
select * from a where id>=1 and id<=70 and type=1 order by tdate desc
这样刚好查询出50条数据

可是不知道第50条数据的ID是多少,该怎么写?怎么来确定id<=?这个值呢
...全文
126 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
nick207 2008-11-25
  • 打赏
  • 举报
回复
select top 50 * from a where id not in(select id from a where type <> 1)
0041123 2008-11-25
  • 打赏
  • 举报
回复
select top 50 * from a where type=1 order by tdate desc
shionco 2008-11-25
  • 打赏
  • 举报
回复
十分感谢,用MSSQL数据库时确实是好使,可我用ACCESS就不好使了
YUYAO2 2008-11-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 virgo2008 的回复:]
使用TOP 可以查处满足条件的前多少条记录,你就不用担心达不到你的要求了,除非他本来就没有50条数据!
[/Quote]
是的
Dogfish 2008-11-25
  • 打赏
  • 举报
回复
先拿出第50的id
然后第二个sql语句去拿你要的数据。

如果数据量大,复合语句有可能慢。
virgo2008 2008-11-25
  • 打赏
  • 举报
回复
使用TOP 可以查处满足条件的前多少条记录,你就不用担心达不到你的要求了,除非他本来就没有50条数据!
wangxiaoning868 2008-11-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sy_binbin 的回复:]
select top 50 * from a where id not in(select id from a where type <> 1)
[/Quote]强,呵呵,路过
lunawzh 2008-11-24
  • 打赏
  • 举报
回复
太简单了,select top 50 * from a where type=1 是取得50条数据,想知道第50条的ID也容易.
set rs=conn("select top 50 * from a where type=1 order by id")
response.write 第50条的ID是:"&rs("id")
sy_binbin 2008-11-24
  • 打赏
  • 举报
回复
select top 50 * from a where id not in(select id from a where type <> 1)

28,404

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧