1

Given the MySQL primary key of a particular element in a paginated result set, is there a function to get the page number that element is in?

If not, what is some pseudocode that can help me get there? Note that primary keys can be deleted and there will be 'gaps' in the sequence so simple arithmetic alone might not work...

4

2 に答える 2

2

このようなもの:

SELECT FLOOR(POSITION/15) FROM (
    SELECT @rownum:=@rownum+1 as POSITION, id 
    FROM table_name, (SELECT @rownum:=0) r
    ORDER BY published DESC
 ) t WHERE id = 17;

(IDが17であり、ページあたりのアイテムが15であり、注文するプロパティがである場合published

ただし、大量のデータが一時的に収集されるため、ソリューション(PHPでさらに実行する場合)は多少無駄になることに注意してください-何らかの方法で現在のページをルーティング/パラメーターの取得/URLを介して受信しようとする可能性があります。

于 2012-11-14T08:46:00.420 に答える
2
SELECT COUNT(*) as amountBefore FROM `table` WHERE `id` <= 'id'

page = ceil(amountBefore / amountPerPage)
于 2012-11-14T08:39:01.130 に答える