fen*_*rol 2 php mysql sql select
我有一些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
Run Code Online (Sandbox Code Playgroud)
我使用此查询调用页面中的图像(例如:test1.php)并给出如下结果:这是我的查询:
select * from gallery where status ='last' order by post DESC
Run Code Online (Sandbox Code Playgroud)
这是我的结果:
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
Run Code Online (Sandbox Code Playgroud)
在页面库中,我进行如下查询:
select * from gallery order by post desc
Run Code Online (Sandbox Code Playgroud)
并给出如下结果:
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
Run Code Online (Sandbox Code Playgroud)
我想要实现的是这样的:
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
Run Code Online (Sandbox Code Playgroud)
最后的结果会变成这样
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
Run Code Online (Sandbox Code Playgroud)
我想知道图像的正确位置,因为在第二页(例如:gallery.php)中,我有很多图像.在第一页(test1.php)中,我只选择5 img,状态为last,并按DESC顺序排序.我想链接到gallery.php页面,我需要一个正确的位置.当我得到一个正确的位置,我可以链接到每个图像,它将是这样的:
<a href='http://localhost/testing/gallery/<?=$result[position]?>.htm'><img src='http://localhost/testing/<?=$result[source]?>.jpg' /></a>
Run Code Online (Sandbox Code Playgroud)
//或者在html中会是这样的
<a href='http://localhost/testing/gallery/1.htm'><img src='http://localhost/testing/img6.jpg' /></a>
Run Code Online (Sandbox Code Playgroud)
所以,如果我能得到正确的行位置,我可以创建一个直接指向页面的链接.
谁能告诉我怎样才能实现这一目标?我以前会感激你的回答
SELECT a.*, @row:=@row+1 AS `Position`
FROM gallery a, (SELECT @row:=0) s
WHERE status = 'last'
ORDER BY post DESC
Run Code Online (Sandbox Code Playgroud)
更新1
SELECT *
FROM
(
SELECT a.*, @row:=@row+1 AS `Position`
FROM gallery a, (SELECT @row:=0) s
ORDER BY case when status = 'new' then 0 else 1 END ASC,
post DESC, id_gallery ASC
) a
WHERE status = 'last'
Run Code Online (Sandbox Code Playgroud)