0

我想通过它们的唯一 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 个唯一 ID,并愿意接受更好的方法?

4

2 回答 2

3

通常的方法是:

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

您可以阅读从 MySQL 数据库表中选择随机记录的说明。

对于您的代码:

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);")
于 2012-08-28T12:47:25.690 回答
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);
于 2012-08-28T13:05:16.997 回答