【问题标题】:Codeigniter Active Record, Multiple where conditionsCodeigniter Active Record,多个 where 条件
【发布时间】:2016-06-02 12:52:03
【问题描述】:

这是我的表结构。我想在提供的日期范围内获取记录。

|--------------|---------------------|
|      id      |   year   |  month   |
|--------------|---------------------|
|      1       |    2015  |  01      |
|      2       |    2015  |  02      |
|      3       |    2016  |  02      |
|--------------|---------------------|

我尝试了以下查询,但没有奏效。

return $this->db->select('*')
                ->from('tourist T')
                ->where('T.year >=', $where['s_year'])
                ->where('T.month >=', $where['s_month'])
                ->where('T.year <=', $where['e_year'])                            
                ->where('T.month <=', $where['e_month'])
                ->get()->result();

【问题讨论】:

  • 你得到了什么输出?
  • 使用$this-&gt;db-&gt;last_query() 打印您的查询并检查您的查询返回什么
  • 查询输出 SELECT T.id FROM (tourist T) WHERE T.year >= 2015 AND T.month >= 1 AND @987645 @.year T.month
  • 以 03 01 格式查看月份
  • 添加单引号和带 0 前缀的月份

标签: php mysql codeigniter activerecord


【解决方案1】:

试试这个:

   $this->db->select('*')
            ->from('test T')
            ->where('T.year >='.$where['s_year'].' and T.month >='.$where['s_month'])
            ->where('T.year <='.$where['s_year'].' and T.month <='.$where['s_month'])
            ->get()->result();

【讨论】:

  • 这是查询输出 SELECT T.* FROM (tourist T) AND T.year >=2015 and T.month >=1 AND T.@ 987654327@
  • 你需要使用括号:SELECT T.* FROM (tourist T) where (T.year >=2015 and T.month >=1) AND (T.year
  • 感谢您的回复,如果我想添加其他 where 子句如 $this->db->select('*') ->from('test T')->where(T .country_id, '13') ->where('T.year >='.$where['s_year'].' and T.month >='.$where['s_month']) ->where('T .year get()->result();
  • 你需要这样做:$this->db->select('*') ->from('test T') ->where('T.country_id =13 and T. year >='.$where['s_year'].' and T.month >='.$where['s_month']) ->where('T.country_id =13 and T.year get()->result();
  • 谢谢你,拯救了我的一天。
猜你喜欢
  • 2015-09-17
  • 2013-09-26
  • 1970-01-01
  • 2013-02-27
  • 1970-01-01
  • 2013-05-04
  • 1970-01-01
  • 1970-01-01
  • 2015-01-04
相关资源
最近更新 更多