【问题标题】:Adding conditional where clause in codeiginter activerecord在 codeigniter activerecord 中添加条件 where 子句
【发布时间】:2014-07-11 19:56:47
【问题描述】:

我正在尝试构建一个查询来搜索使用 Codeigniter 开发的网站中的可用酒店。 我有两个单独的表,“Hotels”用于存储酒店详细信息,“Calendar”用于存储日期和可用性。
“Hotels”表有 id、hotel_title、details
“日历”表有 id(酒店 id)、日期、状态(已预订/可用)

这是我获取特定日期可用酒店的代码

if($this->input->get('date')){
    $this->db->where('calendar.date' , $this->input->get('date'));
    $this->db->where('calender.status !=' , 'booked');
}
$this->db->select('hotels.id , hotels.title, hotels.details');
$this->db->from('hotels');
$this->db->join('calender' , 'hotels.id = calendar.id' , 'left outer');
$this->db->get()->results();

假设有一家酒店于 2014 年 7 月 25 日预订,并于 2014 年 7 月 26 日入住。 当我们搜索 2014-07-25 时,它不会出现在结果中,当我们搜索 2014-07-26 时,它会出现。问题是,如果酒店在某个日期没有保存在日历表中的状态,我希望酒店默认可用(出现在结果中),例如,现在如果我在 2014 年 7 月 27 日搜索该酒店(那里没有行保存它具有此特定酒店的日期和 ID 的“日历”),它不会出现在结果中。
我将不胜感激任何建议。
谢谢

【问题讨论】:

    标签: mysql codeigniter activerecord codeigniter-2


    【解决方案1】:

    只需将您的 where 条件替换为 join on 子句条件即可。

    if($this->input->get('date')){
       $this->db->join('calender' , 'hotels.id = calendar.id AND calendar.date={$this->input->get('date')} AND calender.status != "booked"' , 'left outer');
    }else{
      $this->db->join('calender' , 'hotels.id = calendar.id' , 'left outer');
    }
    

    这是示例代码。根据你的需要改变

    【讨论】:

    • 不幸的是,即使预订了,它也会显示结果
    猜你喜欢
    • 2016-12-16
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多