从表中获取CodeIgniter中某些列中包含相同值的所有行


Getting all rows from the table that contain the same values in some columns in CodeIgniter

现在,我可以通过在CodeIgniter:中使用以下语句从名为cars的表中获取所有值

$this->db->get('cars')->results();

问题是,我想根据名为slug的列中的相同值选择ONLY行,该列是一个存储的URL,例如"非常好的待售汽车"或"黑野马状况良好"。基本上,我想找到所有重复的(但我想稍后将其扩展到名称、描述等,但我现在主要关心的是段塞柱)

在CodeIgniter环境中,我的DB查询应该如何才能只获取重复项,我指的是表中至少两次具有相同slug的行?

是否可以通过CI活动记录而不是通过自定义SQL查询?

首先获取重复记录的ID,然后将此结果用作子查询。

$this->db->select('`id`');
$this->db->from('cars');
$this->db->group_by('slug');
$this->db->having('count(slug) > 1');
$where_clause = $this->db->get_compiled_select();
$this->db->get('cars');
$this->db->where('`id` IN ($where_clause)', NULL, FALSE);