来自top200的mysql随机条目

时间:2015-08-09 16:03:56

标签: php mysql

有没有更好的方法从TOP 200畅销T恤(shop_tshirts的tshirt_sales)返回随机条目,只能查询6个条目而不是200个条件?

$SQL = "SELECT * FROM shop_tshirts WHERE shop = 'nidieunimaitre' AND online='1' ORDER BY tshirt_sales DESC LIMIT 200";

$Result = mysql_query($SQL)
    or die('A error occured: ' . mysql_error());

$Rows = array();

while ($Row = mysql_fetch_assoc($Result))
    $Rows[] = $Row;

shuffle($Rows);

$i = 0;
foreach($Rows as $Data){
$i++;
    if($i >= 6) { break; }
}

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT * 
FROM 
      (SELECT * 
       FROM shop_tshirts 
       WHERE shop = 'nidieunimaitre' AND online='1' 
       ORDER BY tshirt_sales DESC 
       LIMIT 200) as tableAlis
ORDER BY RAND() 
LIMIT 6;

返回前200个结果,然后按rand排序,只返回6个结果。