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

时间:2019-05-28 19:39:53

标签: 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

1 个答案:

答案 0 :(得分:1)

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

请参阅Query Builder Class

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();