2

我在使用 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>

所以,如果我能得到正确的行位置,我可以建立一个正确指向页面的链接。

谁能告诉我我怎样才能做到这一点?我会很感激你的回答,谢谢

4

3 回答 3

6
SELECT a.*, @row:=@row+1 AS `Position`
FROM   gallery a, (SELECT @row:=0) s
WHERE  status = 'last' 
ORDER  BY post DESC

更新 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'
于 2013-01-03T11:08:59.133 回答
0

在您的 php 代码中执行此操作(而不是在 SQL 的临时生成表中添加新行):

$query = mysql_query("YOUR QUERY");
$counter = 0;
while($rows = mysql_fetch_assoc($query)){
     $counter++;
     echo "<a...............".$counter.">.html'><img..../></a>";

}
于 2013-01-03T11:09:57.673 回答
0

最简单的方法可能是按您想要的列(例如位置、帖子、状态...)对查询进行分区,然后在分区内排序。分析函数会有所帮助 - 文档和网络上有很多示例。为位置添加 Row_Number() 或 Rank()...:

SELECT * FROM
( 
 SELECT deptno, ename, sal
    , ROW_NUMBER() OVER (PARTITION BY deptno ORDER BY sal desc) Position_Within_Dept
    , RANK () OVER (PARTITION BY deptno ORDER BY sal desc) ranks
    , DENSE_RANK () OVER (PARTITION BY deptno ORDER BY sal desc) d_ranks
   FROM emp_test
)
--WHERE rno = 3
 ORDER BY deptno, sal DESC
/

发布创建表和插入脚本总是一个好主意。没有人有时间为你做这件事。

于 2013-01-03T13:46:57.137 回答