我在使用 mysql 查询时遇到了一些问题。我只是想知道,如何在 mysql 中获得有条件的行位置。让我解释一下我想要实现的目标。
我有一个表格画廊,它看起来像这样:
id_gallery source status post
1 img1 last 2012/12/11
5 img2 new 2013/01/01
7 img3 new 2013/01/01
10 img4 last 2012/12/11
22 img5 last 2012/12/14
30 img6 last 2012/12/15
我使用此查询调用页面中的图像(例如:test1.php)并给出如下结果: 这是我的查询:
select * from gallery where status ='last' order by post DESC
这是我的结果:
id_gallery source status post
30 img6 last 2012/12/15
22 img5 last 2012/12/14
10 img4 last 2012/12/11
1 img1 last 2012/12/11
在页面库中,我进行了这样的查询:
select * from gallery order by post desc
并给出这样的结果:
id_gallery source status post
5 img2 new 2013/01/01
7 img3 new 2013/01/01
30 img6 last 2012/12/15
22 img5 last 2012/12/14
1 img1 last 2012/12/11
10 img4 last 2012/12/11
我想要实现的是这样的:
id_gallery source status post position
5 img2 new 2013/01/01 1
7 img3 new 2013/01/01 2
30 img6 last 2012/12/15 3
22 img5 last 2012/12/14 4
1 img1 last 2012/12/11 5
10 img4 last 2012/12/11 6
最后的结果会变成这样
id_gallery source status post position
30 img6 last 2012/12/15 3
22 img5 last 2012/12/14 4
1 img1 last 2012/12/11 5
10 img4 last 2012/12/11 6
我想知道图像的正确位置,因为在第二页(例如:gallery.php)中,我有很多图像。在第一页(test1.php)中,我只选择最后一个状态的 5 个 img 并按 DESC 后排序。我想链接到页面gallery.php,我需要一个正确的位置。当我得到一个正确的位置时,我可以为每个图像建立一个链接,它会是这样的:
<a href='http://localhost/testing/gallery/<?=$result[position]?>.htm'><img src='http://localhost/testing/<?=$result[source]?>.jpg' /></a>
//或者在html中会是这样的
<a href='http://localhost/testing/gallery/1.htm'><img src='http://localhost/testing/img6.jpg' /></a>
所以,如果我能得到正确的行位置,我可以建立一个正确指向页面的链接。
谁能告诉我我怎样才能做到这一点?我会很感激你的回答,谢谢