随机播放MYSQL结果 [英] shuffle random MYSQL results

查看:67
本文介绍了随机播放MYSQL结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码显示随机数据库图像以及一个特定的图像(这是我想要的).在数据库查询之后,由于始终首先显示图像ID 11,我该如何洗牌这些结果?我希望图像ID 11随机显示在其他图像之中.

The following code displays random database images as well as one specific image (which is what I want). How can I shuffle these results after database query as image ID 11 is always displayed first? I want image ID 11 displayed randomly amongst the others.

我可以使用SHUFFLE()吗?如果是这样,那么我现在该把它放在哪里?

Can I use SHUFFLE(). If so where exactly do I put it as at the moment?

任何帮助仍然可以学习PHP

Any help would be appreciated as still learning PHP

谢谢.

<?php 

mysql_connect("", "", "") or die(mysql_error()) ; 
mysql_select_db("images") or die(mysql_error()) ;


$photo=mysql_query("SELECT * FROM `profile_images` ORDER BY (ID = 11) DESC, RAND()      
LIMIT 7");


while($get_photo=mysql_fetch_array($photo)){ ?>

<div style="width:300px;">


<img src="<? echo $get_photo['url']; ?>">


</div>

<? } ?>

推荐答案

在将它们检索到php后,您可以对其进行洗牌.

You can shuffle them after they are retrieved to php.

$photos = array();
while ($get_photo = mysql_fetch_array($photo))
    $photos[] = $get_photo;

shuffle($photos);

或者您可以通过子查询来做到这一点:

Or you can do it with subqueries:

SELECT A.* FROM (
    SELECT * FROM `profile_images` 
    ORDER BY (ID = 11) DESC, RAND()      
    LIMIT 7
) as A
ORDER BY RAND()

这篇关于随机播放MYSQL结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆