如何在Codeigniter中查找重复记录

时间:2019-03-15 07:24:55

标签: php mysql duplicates codeigniter-3

我正在尝试编写查询以在 Codegigniter 中查找重复的行,但是它不起作用。 我的代码(型号)

$this->db->select('*');
      $this->db->from('myTable sr');
      $this->db->join('table2 s','sr.st_id=s.st_id'); 
      if($conn!='')
      {
          $this->db->where($conn);
      }
      if($identity!='')
      {
          $where = "(s.student_id = '$identity' OR s.name LIKE '%$identity%')";
          $this->db->where($where);
      }
      if($duplicate != '')
      {
        $this->db->having('COUNT("sr.st_id")>1');          
      }
      if($limit_row!='')
      {
          $this->db->limit($limit_row,$starting);
      }
      $query = $this->db->get();
      var_dump($this->db->last_query($query));exit;
      return $query->result_array();

仅返回第一条记录 重复的数据在myTable中,我想找到找到重复的数据

2 个答案:

答案 0 :(得分:0)

SELECT username, email, COUNT(*)
FROM users
GROUP BY username, email
HAVING COUNT(*) > 1

分组并找到它

答案 1 :(得分:0)

$this->db->select('*');
$this->db->from('myTable sr');
$this->db->join('table2 s','sr.st_id=s.st_id'); 
if($conn!='')
{
    $this->db->where($conn);
}
if($identity!='')
{
    $where = "(s.student_id = '$identity' OR s.name LIKE '%$identity%')";
    $this->db->where($where);
}
if($duplicate != '')
{
  $this->db->having('COUNT("sr.st_id")>1');
  $this->db->group_by('sr.st_id');          
}
if($limit_row!='')
{
    $this->db->limit($limit_row,$starting);
}
$query = $this->db->get();
var_dump($this->db->last_query($query));exit;
return $query->result_array();