使用 where 子句时如何避免结果数组中的重复行

How to avoid duplicated rows from my result array when using where clause

我需要应用我从我的控制器发送的时间和 time = double 的用户的名称,所以我需要显示这个函数的结果但它重复了结果,有没有办法解决这个问题?

特定日期选择time=double的用户名,每天显示。

public function get_Shifts($day,$time){

    $this->db->select('user.fullname');
    $this->db->from('shifts');
    $this->db->join('user', 'shifts.user_id = user.id', 'left');
    $this->db->where('day=', $day);
    $this->db->where('time=',$time);
    $this->db->or_where('time=', $double);
    $query = $this->db->get();

    return $query->result_array();
}

我尝试添加这一行但没有帮助:$this->db->distinct();

从 Codeigniter 3 开始,您可以使用它的 Query Grouping 功能:

Query grouping allows you to create groups of WHERE clauses by enclosing them in parentheses. This will allow you to create queries with complex WHERE clauses

在你的情况下,你可以使用:

$this->db->group_start()
  $this->db->where('day', $day);
  $this->db->where('time', $time);
$this->db->group_end()
$this->db->or_where('time', $double);

这将生成如下内容:

WHERE ( (`day` = '2020-04-23' AND `time` = '16:00'  ) OR `time` = true )

编辑以回应评论: 创建如下输出:WHERE ( (day = '2020-04-23' AND (time = '16:00' OR time = true ) )

你使用这种方法:

$this->db->where('day', $day);
$this->db->group_start()
  $this->db->where('time', $time);
  $this->db->or_where('time', $double);
$this->db->group_end()

注意: CI where() 函数的正确使用方法是:

$this->db->where('day', $day); 而不是 $this->db->where('day=', $day);

使用时应该有错误

$this->db->where('day=', $day);

$this->db->where('time=',$time);

$this->db->or_where('time=', $double);

where子句的正确使用方法

$this->db->where('day', $day);

$this->db->where('time',$time);

$this->db->or_where('time', $double);

如果您为上述查询获取重复数据,请将列名传递给不同的数据,例如:-

$this->db->distinct('user.fullname');