从sql表中选择随机行时如何防止选择已删除的ID

时间:2012-08-28 12:44:49

标签: php mysql sql database

我想通过其唯一ID来从产品数据库中选择随机行。 我需要每页3-4个随机产品...

这是我开始的方式

$offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `products` ");
$offset_row = mysql_fetch_object( $offset_result ); 
$offset = $offset_row->offset;
$result = mysql_query( " SELECT * FROM `products` WHERE id = $offset LIMIT 1" );
$n=mysql_num_rows($result);

if ($n>0){
    while($row = mysql_fetch_array($result)){ 
        $id = $row["id"];
        $cod_produs = $row["cod_produs"];
        $nume_produs = $row["nume_produs"];
        $titlul_paginii = $row["titlul_paginii"];
        $nume_intermediar = str_replace (" ", "-", $nume_produs);
        $nou_nume_produs = strtolower($nume_intermediar);
        $detalii = $row["detalii"];
        $seo_descriere = $row["seo_descriere"];
        $materiale = $row["materiale"];
        $numere = $row["numere"];
        $pret = $row["pret"];
        $categorie = $row["categorie"];
        $subcategorie = $row["subcategorie"];
        $data_adaugare = strftime("%b %d, %Y", strtotime($row["data_adaugare"]));

    }

echo ("The id of the product is ".$id." and his name is ".$nume_produs);

    };
  ?>

问题是我的代码返回了我随时间删除的ID ...我如何阻止这样做?

我如何一次性返回3个或4个唯一身份证,并且愿意接受更好的方法吗?

2 个答案:

答案 0 :(得分:3)

通常的方式是:

SELECT * FROM `table` ORDER BY RAND() LIMIT 0,1;

您可以在Selecting random record from MySQL database table.

阅读说明

代码:

mysql_query( " SELECT * FROM `products` ORDER BY RAND() LIMIT 0,3" );

编辑过期OP评论

然后,您可以获得数据的子集。修改你的代码:

$offset_result = mysql_query( "SELECT FLOOR(MAX(id) * RAND() - 1000) AS `offset` 
                               FROM `products` ");
$offset_row = mysql_fetch_object( $offset_result ); 
$offset = $offset_row->offset;
$result = mysql_query( " SELECT * FROM `products` 
                         WHERE id between $offset and $offset + 1000
                         ORDER BY RAND() 
                         LIMIT 0,3" );

快速解决方案是获得3个随机ID:

$randoms_id = '-1';
for($i=0;$i<3;$i++) {

  $id_result = mysql_query( " SELECT id FROM `products` 
                              WHERE id >= 
                                  (SELECT FLOOR( MAX(id) * RAND()) 
                                   FROM `products` ) 
                              ORDER BY id LIMIT 1;");
  $id_row = mysql_fetch_object( $id_result ); 
  $id = $id_row->id;

  $randoms_id = $randoms_id . ',' . $id;
}
$result = mysql_query( " SELECT * FROM `products` 
                         WHERE id in ($randoms_id);")

答案 1 :(得分:1)

您的脚本中有错,请执行以下操作:

$offset_result = mysql_query("SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM `products`");
$offset_row = mysql_fetch_object($offset_result); 
$offset = $offset_row->offset;
$result = mysql_query("SELECT * FROM `products` LIMIT $offset, 1");
$n=mysql_num_rows($result);

但这只会返回一条记录。您可以获得连续记录,或者您必须多次运行。

$offset_result = mysql_query("SELECT COUNT(*) AS total FROM `products`");
$offset_row = mysql_fetch_object($offset_result); 

$total = (($offset_row->total) - 1);
$range_arr = range(0, $total);
shuffle($range_arr);
$off_1 = $range_arr[0];
$off_2 = $range_arr[1];
$off_3 = $range_arr[2];

$result = mysql_query(" (SELECT * FROM `products` LIMIT $off_1, 1)
                        UNION
                        (SELECT * FROM `products` LIMIT $off_2, 1)
                        UNION
                        (SELECT * FROM `products` LIMIT $off_3, 1)");
$n=mysql_num_rows($result);