简体   繁体   English

日期比较查询php sql

[英]Date comparison query php sql

I'm struggling to compare dates in an sql query on 2 counts - I've looked around at strtotime / date / from unixtime etc, but can't quite work it out. 我正在努力比较2个查询中的SQL查询中的日期-我已经在strtotime / date / unixtime等中四处查看,但是不能完全解决。

$currentday is a date string in the format of 2013-12-29 $currentday是日期字符串,格式为2013-12-29

Enddate is a 'date' type sql string - in the format 0000-00-00 (sometimes with dates and other times null ) Enddate是“日期”类型的sql字符串-格式为0000-00-00 (有时带有日期,其他时候为null

  1. I only want to retrieve those dates where the current date is before the end date - but the sql query I have is returning all of them. 我只想检索当前日期早于结束日期的那些日期,但是我拥有的sql查询返回了所有这些日期。
  2. I know it is slower, but I tried to achieve it in the code - this worked... however I found another problem. 我知道它的速度较慢,但​​是我尝试在代码中实现它-这确实有效...但是我发现了另一个问题。 I want to be able to say if 'enddate' is after the current date OR enddate is null (0000-00-00) then print.. etc - However I can't get the null to work. 我想说的是'enddate'是在当前日期之后还是enddate为null(0000-00-00),然后打印...。等等-但是我无法使null起作用。

Here is the code: 这是代码:

///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/>';  
    }
  }
}

您必须引用字符串:

WHERE enddate >= '".$currentday."' AND ...

Unclear what the end goal is, but have adjusted your MySQL to accommodate 0000-00-00 and NULL dates. 不清楚最终目标是什么,但是已经调整了MySQL以适应0000-00-00NULL日期。

$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
");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM