2012-08-28 39 views
0

我想通過其唯一的ID從產品數據庫中選擇隨機行。 我需要3-4個隨機產品的每一個頁面上...如何防止在從sql表中選擇隨機行時選擇已刪除的ID

我這是怎麼開始

$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); 

    }; 
    ?> 

的問題是,我的代碼返回我已經隨着時間的推移刪除IDS ...我如何防止這樣做?

我怎樣才能一次返回3個或4個獨特的ID,並且可以更好地實現這個目標?

+0

你怎麼知道你的產品是否被刪除?表中的字段/列名稱顯示它是否被刪除了什麼? – Gustonez

+0

我沒有那張桌子......所以我需要用我所有的產品ID創建一張桌子,以便刪除或刪除我的產品ID。 – user1483138

+0

不,只是添加一個新的列名稱'刪除',並設置值1刪除產品。我會在這裏發佈答案 – Gustonez

回答

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); 
+0

我不想使用rand的訂單..因爲非常慢..並且不推薦使用大表格... – user1483138

+0

哦!好吧,現在很清楚。你應該清楚你的問題。我會編輯我的答案。 – Gustonez

+0

對不起,不清楚..我是新來這個域名 – user1483138

3

的usualy方法是:

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

正如我所知ORDER BY RAND()對於大表格來說是緩慢的...並且它不會被recomanded使用... – user1483138

+0

@ user1483138,fixed in回答。對不起,PHP語法錯誤,不適用於PHP。 – danihp

+0

謝謝你的回答,你有一個錯誤...我修正它在$ id = ...它必須是$ id = $ id_row-> id ...第一部分它的工作很好..但它只返回小值...第二部分...返回某個時間相同的ID兩次...謝謝。我設法解決了問題 – user1483138