Select 来自 table 的随机记录数,其中列值在 mysql 中的结果集中不重复
Select a random number of records from a table where column value does not repeat for the result set in mysql
有一个带有 "tag" 列的 table - 我想从我的 table 中 select 10 条随机记录,其中 10 条记录中的 none 共享相同的标签(每条记录都有一个唯一的标签)。我该怎么做?这是我当前的查询:
$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
and/or a GROUP BY
。
根据手册所述:
例如:
$this->db->distinct();
and/or
$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;
有一个带有 "tag" 列的 table - 我想从我的 table 中 select 10 条随机记录,其中 10 条记录中的 none 共享相同的标签(每条记录都有一个唯一的标签)。我该怎么做?这是我当前的查询:
$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
and/or a GROUP BY
。
根据手册所述:
例如:
$this->db->distinct();
and/or
$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;