我想在两个日期之间获取销售数据,但它无法正常运行SQL查询。
if($_GET['startdate'])
{
$startdate = date('Y-m-d H:i:s', strtotime($_GET['startdate']));
}
else
{
$startdate = 'NOW()';
}
if($_GET['enddate'])
{
$enddate = date('Y-m-d H:i:s', strtotime($_GET['enddate']));
}
else
{
$enddate = date('Y-m-d H:i:s', strtotime('-1 month'));
}
$db->setTrace('true');
$sales = $db->rawQuery('SELECT * from sales where (saledate BETWEEN ? AND ?) AND deletestat = ?', [$startdate, $enddate, '0']);
print_r($db->trace);
如何在两个日期之间获取数据?
答案 0 :(得分:1)
函数之间的mysql与你期望的不同。
between函数期望第一个参数为最小值,第二个参数为最大值。请参阅文档:http://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_between
如果您更改此行:
$sales = $db->rawQuery('SELECT * from sales where (saledate BETWEEN ? AND ?) AND deletestat = ?', [$startdate, $enddate, '0']);
对此:
$sales = $db->rawQuery('SELECT * from sales where (saledate BETWEEN ? AND ?) AND deletestat = ?', [$enddate, $startdate, '0']);
它会起作用。