php - Codeigniter 在哪里使用 AND
问题描述
我想用这样的codeigniter构建一个查询。
SELECT * FROM
table
WHERE 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
解决方案
请尝试以下代码:-
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();
}
}
推荐阅读
- groovy - (error) pipeline jenkins, invoke sh in a loop + serialization json
- scala - 从套接字源数据创建数据帧时如何指定shema?
- ruby-on-rails - Rails 多个嵌套表单
- amazon-web-services - Spinnaker 创建应用程序
- azure-devops - 不能再创建和编辑服务挂钩
- python - Python 软件开发中的 stdafx.h 等价物
- string - 将 STR、FLOAT 或 ATTR 组合到 Tableau 中的计算字段中
- sql - 如果 Linq 中的值不为空,则仅包含在 where 条件中
- arduino - 如何在没有计算机的情况下仅使用数字引脚在 Arduino 上读取键盘输入
- ruby-on-rails - 即使手动一切正常,Rspec 测试也会失败