我想在mysql中的两个日期之间检索数据.
从日期:01/04/2015
到目前为止:01/05/2015
但我不能在单变量中得到这些日期;
我如何得到如下所述:
$fdate=01;
$fmonth=04;
$tdate=01;
$tmonth=05;
$year=2015;
Run Code Online (Sandbox Code Playgroud)
我在模型中的代码:
function date_range($fdate,$fmonth,$tdate,$tmonth,$year)
{
$this->db->select('*');
$this->db->where('DAY(order_date) >=',$fdate);
$this->db->where('MONTH(order_date) >=',$fmonth);
$this->db->where('YEAR(order_date) >=',$year);
$this->db->where('DAY(order_date) <=',$tdate);
$this->db->where('MONTH(order_date) <=',$tmonth);
$this->db->where('YEAR(order_date) <=',$year);
return $this->db->get('orders');
}
Run Code Online (Sandbox Code Playgroud)
有时候它会正确地返回结果,有时它不会返回结果,但我mysql在提到的两个日期之间有一个数据.
我对这段代码犯了什么错误请帮助我
您可以使用
$this->db->where("YOUR COLUMN BETWEEN DATE_FORMAT(COLUMN1,'%d/%m/%Y') AND DATE_FORMAT(COLUMN2,'%d/%m/%Y'"));
Run Code Online (Sandbox Code Playgroud)
您可以使用 CodeIgniter:
$query = $this->db->query("YOUR QUERY");
Run Code Online (Sandbox Code Playgroud)
"Your QUERY"你将包含 mysql 的地方DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
例如使用查询绑定
sql = "SELECT * FROM some_table
WHERE order_date between DATE_FORMAT(? ? ?,'%d %m %Y')
AND DATE_FORMAT(? ? ?,'%d %m %Y')";
$this->db->query($sql, array($fdate, $fmonth, year, $tdate, $tmonth, year));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6265 次 |
| 最近记录: |