首页 > 解决方案 > Codeigniter 在哪里使用 AND

问题描述

我想用这样的codeigniter构建一个查询。

SELECT * FROM tableWHERE machine=$var AND date BETWEEN '$today' AND '$oneMonthAgo'

这是我的模态

public function getHistory($machine)
    {
    $today = date('Y-m-d');

    $month=date('Y-m-d',strtotime("-1 month"));
    $query = $this->db->from('table')->where('machine', $machine)->where("date BETWEEN '$today' AND '$month'")->get();

        if ($query->num_rows() > 0) {
            return $query->row();
        }
    }

我一直在尝试,但我没有得到结果加上:用这条线可以计算一个月前的日期

$today = date('Y-m-d');

$month=date('Y-m-d',strtotime("-1 month"));

对不起,我的英语 xP

标签: phpcodeigniter

解决方案


请尝试以下代码:-

public function getHistory($machine)
{
    $today = date('Y-m-d');
    $query = $this->db->from('table')
             ->where('machine', $machine)
             ->where('date >=', (NOW() - INTERVAL 1 MONTH))
             ->get();
    if ($query->num_rows() > 0) 
    {
      return $query->result();
    }
}

推荐阅读