Home  >  Article  >  Backend Development  >  php+mysql 判断记录位置

php+mysql 判断记录位置

WBOY
WBOYOriginal
2016-06-20 12:28:48787browse

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn