快速找到我想要的页面之前和之后的页面

时间:2012-12-02 17:49:58

标签: php mysql performance

我是mysql的初学者,我想知道是否有更快的方法来执行以下操作:

我有一张这样的表:

------------------------------
| Pages |      Creation      |
------------------------------
| bar   | 2012-10-10 10:11:10|
| blah  | 0000-00-00 00:00:00|
| foo   | 2012-10-10 10:10:10|
------------------------------

没有主键。

我想知道之前的页面和特定页面之后的页面(按创建日期排序)。

目前我这样做:

Sql:SELECT * FROM table ORDER BY creation

腓:

foreach($r as $c=>$t)
{
    if($t['pages']==$thepageiwant)
    {
        $before=$c-1;
        $after=$c+1;
        break;
    }
}

1 个答案:

答案 0 :(得分:2)

上一页:

SELECT table.Pages
FROM table, (SELECT Creation FROM table WHERE Pages = "'.$page.'") AS t2
WHERE table.Creation < t2.Creation
ORDER BY  table.Creation DESC
LIMIT 1

下一页:

SELECT table.Pages
FROM table, (SELECT Creation FROM table WHERE Pages = "'.$page.'") AS t2
WHERE table.Creation > t2.Creation
ORDER BY  table.Creation ASC
LIMIT 1