如何将此查询转换为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
答案 0 :(得分:1)
实际上有几种方法可以在Codeigniter中编写查询,如果您刚刚开始使用此框架编写查询,那么从我的角度来看,以下语法是最容易理解的。
SELECT DISTINCT
gp1.account
FROM gp.t2001 gp1
WHERE gp1.transactiontypecode = 8
AND NOT EXISTS (SELECT *
FROM gp.t2001 gp2
WHERE gp2.account = gp1.account
AND gp2.transactiontypecode <> 8);
上面的代码与此代码相同:
$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();