簡體   English   中英

從表中選擇一個隨機數的記錄,該表中的列值不針對mysql中的結果集重復

[英]Select a random number of records from a table where column value does not repeat for the result set in mysql

有一個帶有“標簽”列的表-我想從我的表中選擇10條隨機記錄,其中10條記錄中沒有一個共享相同的標簽(每個記錄都有一個唯一的標簽)。 我該怎么做? 這是我當前的查詢:

$this->db->select('extra_imagery');
$this->db->where('cat', '4');
//need something like: $this->db->where('tag',IS UNIQUE);
$this->db->limit($this->config->item('imagery-limit'));
$this->db->order_by('extra_imagery.id','RANDOM');

為了避免重復,您可以對查詢使用DISTINCT和/或GROUP BY

按照手冊中的規定:

例如:

$this->db->distinct();

和/或

$this->db->distinct();

其他參考(MySQL):

嘗試這個

$query = $this->db->query("SELECT DISTINCT extra_imagery FROM table_name WHERE cat = 4 GROUP BY RAND() id");
$result = $query->result_array();
return $result;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM