【问题标题】:How to find duplicate records in codeigniter如何在codeigniter中查找重复记录
【发布时间】:2019-08-06 05:37:21
【问题描述】:

我正在尝试编写查询以查找 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 中,我想找到这些重复项

【问题讨论】:

  • 请提供数据。没有它,我们无法检查查询。
  • 这个语句不清楚,有bug => $this->db->join('table2 s','sr.st_id=s.st_id'); rs','rs.resultState_id=sr.result_state');
  • 我编辑了代码,目标是如何找到重复的。

标签: php mysql duplicates codeigniter-3


【解决方案1】:
SELECT username, email, COUNT(*)
FROM users
GROUP BY username, email
HAVING COUNT(*) > 1

分组并找到它

【讨论】:

  • 它确实跳过了重复记录,但我想找到重复的记录。Mona Bishnoi
【解决方案2】:
$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();

【讨论】:

  • 它只显示一条记录,我想显示全部。我的意思是一条记录中有多少重复项
猜你喜欢
  • 2019-02-18
  • 1970-01-01
  • 2015-03-25
  • 1970-01-01
  • 2020-02-07
  • 2015-02-14
  • 2010-10-25
  • 2015-01-15
相关资源
最近更新 更多