id catid order
1 1 1
2 1 5
3 2 3
4 1 2
5 2 15
7 1 11
8 1 9
...
152 1 155
153 1 143
我需要的是根据一个id,算出这个id是当前catid下的第几条记录
比如 给的 id 是 7 对应的catid是1 。 sql语句就是:
select * from `news_list` where `tid` =1order by `order` asc limit x , 30
因为根据order排序,而且数据比较多。
需要解决的问题是:
比如给个id=7 , 根据id所属的catid,计算出获取的结果中此id的位置。(7是第4条记录)
希望大神们帮忙。
ps:数据量大,不能先读出满足条件的所有记录,再判断。
我的回答如下:
set @i :=0;select @i :=@i+1 as num,news_list.* from `news_list` where `catid` =1order by `order` asc limit 0 , 30