mysql - 将 mysql 查询转换为 CodeIgniter $query->row

标签 mysql codeigniter-3

如何将此查询转换为 CodeIgniter?

$date = '28-05-2019';
$time_start = '10:00';
$time_end = '19:00';

select courtrooms.* from courtrooms
left join reservations 
on courtrooms.id = reservations.courtroom_id
and reservations.date = '$date' and reservations.time_start < '$time_start' and reservations.time_end > '$time_end'
where reservations.id is null

最佳答案

实际上有多种方法可以在 Codeigniter 中编写查询,如果您刚刚开始使用此框架编写查询,那么从我的角度来看,以下语法是最容易理解的。

请参阅Query Builder Class

$this->db->select('*');
$this->db->from('courtrooms');
$this->db->join('reservations','courtrooms.id = reservations.courtroom_id','left');
$this->db->where('reservations.id IS NULL', null, false);
$this->db->where('reservations.date', $date); 
$this->db->where('reservations.time_start<', $time_start);
$this->db->where('reservations.time_end>', $time_end);
$query = $this->db->get();

$query = $query->result();

上面的代码与下面的代码相同:

$query = $this->db->join('reservations','courtrooms.id = reservations.courtroom_id','left')
                  ->where('reservations.id IS NULL', null, false)
                  ->where('reservations.date', $date)
                  ->where('reservations.time_start<', $time_start)
                  ->where('reservations.time_end>', $time_end)
                  ->get('courtrooms');
$query = $query->result();

关于mysql - 将 mysql 查询转换为 CodeIgniter $query->row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56349037/

相关文章:

php - 代码点火器错误 : variable references

php - CodeIgniter 添加和更新数据库没有成功,没有错误,没有,只是不会工作

php - 使用 where、like 和 or_like CodeIgniter 3 Active Records 进行多重连接

MySQL : only_full_group_by multiple date isn't in group by

php - 无法在sql语句中使用隐藏字段

php - 获取范围内的记录以及范围内的重叠记录

php - Codeigniter 回显 [::1] 而不是 localhost

PHP/MySQL - 跨多个集合分析公共(public)集合

c# - log4net AdoNetAppender 在数据库中插入 "Null"字符串而不是空值

codeigniter - CodeIgniter 3 完成了吗?