简体   繁体   English

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

[英]Convert mysql query to CodeIgniter $query->row

How to convert this query to CodeIgniter? 如何将此查询转换为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

There are actually several ways to write queries in Codeigniter, the following syntaxis is the most understandable from my point of view if you are just starting to write queries using this framework. 实际上,有几种方法可以在Codeigniter中编写查询,如果您刚刚开始使用此框架编写查询,则以下语法从我的角度来看是最容易理解的。

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

The above code is the same than this one: 上面的代码与此相同:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM