基于随机顺序的下一个和上一个MYSQL

时间:2014-01-02 20:40:06

标签: php mysql sql sql-order-by

我有一个包含许多记录的数据库。记录确实有唯一的索引。 我使用以下MYSQL查询来创建随机排序的记录列表:

SELECT  `image_id` ,  `image_name` ,  `image_type` ,  `image_height` ,  `image_width` ,  `image_title` FROM  `images` WHERE  `active` =  '1' ORDER BY RAND($rand_order)
然后将 $ rand_order保存在会话中,以便稍后可以重新创建相同的订单。 在用户点击图像后(我将使用image_id),我想获取下一行和上一行的image_id。

我尝试使用以下查询,但没有工作:

SELECT  `image_id` FROM `images` WHERE  `active` =  '1' AND `image_id` > '$cureent_image_id' ORDER BY RAND($rand_order)

我也尝试获取当前记录的行号,并使用限制来显示其他图像,但不确定如何在用户第一次点击链接时获取该信息而不将其添加到URL。

有关如何获取下一个和上一个ID的任何想法或建议吗?

2 个答案:

答案 0 :(得分:4)

你能做什么,得到你的结果,保存元素的位置:

SELECT  `image_id` ,  ...  FROM  `images` 
    WHERE `active` =  '1' ORDER BY RAND($rand_order)

处理结果时,您可以按顺序保存ID的位置

然后,为了得到下一个,你设置查询的偏移量,这将为你提供该位置的元素

SELECT  `image_id` ,  ..., FROM  `images` 
    WHERE `active` =  '1' ORDER BY RAND($rand_order) offset X limit 1

您可以查看这个小提琴:http://sqlfiddle.com/#!2/5355f/12

答案 1 :(得分:1)

你可以做的是选择rand($ rand_order)作为所选字段之一然后 保存用户点击的Image_id。然后在这个例子中,用户点击的image_id是6.然后使用相同的rand($ rand_order)使用2个底部查询来获取上一个和下一个。

请参阅此sqlFiddle

SELECT  `image_id` ,`image_name`,`image_type`,  
        `image_height`,`image_width`,`image_title`,
        RAND(11) as rowOrder
FROM  `images` 
WHERE  `active` =  '1' ORDER BY rowOrder;

SET @image_id=6; /* your user clicked on this image_id */

SELECT T1.* FROM
(SELECT  `image_id` ,`image_name`,`image_type`,  
        `image_height`,`image_width`,`image_title`,
        RAND(11) as rowOrder
 FROM  `images` 
 WHERE  `active` =  '1'
)T1
 WHERE T1.rowOrder < (SELECT rowOrder FROM
                           (SELECT  `image_id`,
                            RAND(11) as rowOrder
                            FROM  `images` 
                            WHERE  `active` =  '1'
                            )T2
                       WHERE image_id=@image_id
                       )
ORDER BY rowOrder DESC LIMIT 1;

SELECT T1.* FROM
(SELECT  `image_id` ,`image_name`,`image_type`,  
        `image_height`,`image_width`,`image_title`,
        RAND(11) as rowOrder
 FROM  `images` 
 WHERE  `active` =  '1'
)T1
WHERE T1.rowOrder > (SELECT rowOrder FROM
                           (SELECT  `image_id`,
                                     RAND(11) as rowOrder
                             FROM  `images` 
                             WHERE  `active` =  '1'
                            )T2
                      WHERE image_id=@image_id
                      ) 
ORDER BY rowOrder ASC LIMIT 1;

更好的是,如果您保存用户点击的图片的image_idrowOrder来获取您的上一个和下一个,您可以看到sqlFiddle

SELECT  `image_id` ,`image_name`,`image_type`,  
        `image_height`,`image_width`,`image_title`,
        RAND(11) as rowOrder
FROM  `images` 
WHERE  `active` =  '1' ORDER BY rowOrder;

SET @rowOrder=0.424008598946; /* or if you choose to save rowOrder from previous query
                                 of the image that user has clicked on and save the image_id
                              */
SET @image_id=6;

SELECT T1.* FROM
(SELECT  `image_id` ,`image_name`,`image_type`,  
        `image_height`,`image_width`,`image_title`,
        RAND(11) as rowOrder
 FROM  `images` 
 WHERE  `active` =  '1'
)T1
 WHERE T1.rowOrder < @rowOrder
   AND T1.image_id != @image_id
ORDER BY rowOrder DESC LIMIT 1;

SELECT T1.* FROM
(SELECT  `image_id` ,`image_name`,`image_type`,  
        `image_height`,`image_width`,`image_title`,
        RAND(11) as rowOrder
 FROM  `images` 
 WHERE  `active` =  '1'
)T1
WHERE T1.rowOrder > @rowOrder
  AND T1.image_id != @image_id
ORDER BY rowOrder ASC LIMIT 1;

我正在执行上面的T1.image != @image_id因为保存的rowOrder不是一个精确的值,所以当我们与T1.rowOrder > @rowOrder进行比较时,我们可能会返回该行为了确保我们没有收到行,我们只是确保它不等于保存的@image_id