Codeigniter数据库查询,或组合的位置和位置

时间:2017-06-16 22:22:07

标签: php codeigniter query-builder

我正在尝试获得正确的数据库条目组合。

比方说,我有几个事件。我希望所有人能够完成今天结束的活动

$this->db->where('endDate >=', date('Y-m-d'));

但我也想要那些没有约会的人,所以他们可以在TBC

$this->db->or_where('startDate', null);

但最后我只想要那些事件名称为bob的人!

$this->db->where('eventName', 'bob');

但是我在数据库中获取了所有eventName。

我如何只获得BOB事件但是有这些日期选项?

4 个答案:

答案 0 :(得分:0)

关于如何放置查询的全部内容。你可以试试这个

$this->db->where('eventName', 'bob');
$this->db->where('endDate >=', date('Y-m-d'));
$this->db->or_where('startDate', null);

# Syntax
WHERE eventName = 'bob' AND WHERE endDate >= 'xxxx-xx-xx' OR startDate = null

# Result
    - assume endDate is 2017-6-17
eventName       | endDate           | startDate     | Output(Gets/Not)
    Bob         |   2017-6-17       |    null       |       Gets
    Ron         |   2017-6-17       |    null       |       Not
    Bob         |   2017-6-18       |    null       |       Not
    Bob         |   2017-6-15       |    null       |       Gets
    Bob         |   2017-6-15       |    2017-3-25  |       Not      

答案 1 :(得分:0)

您可以像这样使用

$this->db->where('endDate >=', date('Y-m-d'))->or_where('startDate', null);
$this->db->where('eventName', 'bob');

答案 2 :(得分:0)

感谢Abdulla和Muhammad,我找到了解决方案。我这样做似乎有效(在尝试了所有可能的组合之后)

        $this->db->where('eventName', 'bob');
        $this->db->where('startDate', NULL)->or_where('endDate >=', date('Y-m-d'));
        $this->db->where('eventName', 'bob');

所以我顶了尾巴。我知道它不是DRY但是meh,它是唯一可行的。奇怪的。我的得分不到15分,所以不能给他们投票。

答案 3 :(得分:0)

您可以尝试以下操作:

$this->db->where(" (end_date >= '".date('Y-m-d')."' OR start_date = '' ) AND eventName = 'bob' ");