Php 在mysql中获取具有条件的行位置

Php 在mysql中获取具有条件的行位置,php,mysql,sql,select,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

我在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
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
我使用此查询调用页面(ex: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
在页面库中,我进行如下查询:

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
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
   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
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
   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
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
<a href='http://localhost/testing/gallery/1.htm'><img src='http://localhost/testing/img6.jpg' /></a>
我想知道图片的正确位置,因为在第二页(例如:gallery.php),我有很多图片。在第一个页面(test1.php)中,我只选择状态为last的5个img,并按post DESC排序。我想链接到page gallery.php,我需要一个正确的位置。当我得到一个正确的位置,我可以做一个链接到每个图像,它会是这样的:

<a href='http://localhost/testing/gallery/<?=$result[position]?>.htm'><img src='http://localhost/testing/<?=$result[source]?>.jpg' /></a>

//或者在html中是这样的

          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
<a href='http://localhost/testing/gallery/1.htm'><img src='http://localhost/testing/img6.jpg' /></a>

因此,如果我能得到一个正确的行位置,我就能创建一个正确指向页面的链接

有谁能告诉我如何才能做到这一点? 谢谢你的回答,谢谢

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'
在php代码中执行此操作(而不是在SQL的临时生成表中添加新行):

$query=mysql\u query(“您的查询”);
$counter=0;
while($rows=mysql\u fetch\u assoc($query)){
$counter++;
echo“.html'>”;
}

最简单的方法可能是按所需的列(如职位、职位、状态)对查询进行分区。。。然后在分区内按顺序排序。分析功能将有所帮助-文档和web上有许多示例。为position或Rank()添加行号():


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

准确地说:你还需要
设置@row=0在程序的开始处query@JW谢谢你的回答,这是工作。但我又有一个问题,如何检索最后状态的img位置,我刚刚编辑了我的问题,请检查一下。。