我很难在sql查询中比较2个计数的日期 - 我在strtotime
/日期/从unixtime等处查看过,但是不能完全解决它。
$currentday
是日期字符串,格式为2013-12-29
Enddate是一个'日期'类型的sql字符串 - 格式为0000-00-00
(有时带有日期和其他时间null
)
以下是代码:
///Find all regular payments ordered by amount descending
$check = mysql_query("
SELECT *
FROM
regular
WHERE
enddate >= ".$currentday." AND
userid = ".$varuserid."
ORDER BY amount desc
");
if($check === FALSE) {
die(mysql_error()); // TODO: better error handling
} else {
$regcheck = array(); //Assign details of valid regular payments for the user to an array
while($val=mysql_fetch_array($check)) {
//SHOULD BE LIMITED BY SQL QUERY - Don't understand why it isnt??
if (($val['enddate'] > $currentday) || (!$val['enddate'])) {
print $val['enddate'].'?'.$currentday.'<br/>';
//ERROR - NOT SURE WHY THIS SHOWS UP FROM THE SQL QUERY?
} else {
print 'err'.$val['enddate'].'?'.$currentday.'<br/>';
}
}
}
答案 0 :(得分:1)
你必须引用字符串:
WHERE enddate >= '".$currentday."' AND ...
答案 1 :(得分:0)
不清楚最终目标是什么,但调整了MySQL以适应0000-00-00
和NULL
日期。
$check = mysql_query("
SELECT *
FROM
regular
WHERE
(enddate >= '" . $currentday . "' AND
enddate IS NOT NULL AND
enddate != '0000-00-00') AND
userid = ".$varuserid."
ORDER BY amount desc
");