【问题标题】:How to select rows between date and time in mysql?如何在mysql中选择日期和时间之间的行?
【发布时间】:2014-07-05 18:56:00
【问题描述】:

有一个包含三列的表。 ID、日期和时间。我需要选择 2014/01/04 和 2014/06/15(由绿色指定)之间的行,其中开始时间为 12:00:00,完成时间为 20:00:00(由蓝色指定)。如何选择所有蓝色行?

我用了这个方法:

$query = "SELECT * FROM $database_table 
WHERE UTCdate BETWEEN '{$from_date}' AND '{$to_date}' 
AND satellite_derived_time BETWEEN CAST('{$from_time}' AS UNSIGNED) 
AND CAST('{$to_time}' AS UNSIGNED)";

但这是不对的,因为时间并不总是像日期一样增加。

我需要日期在 2014/01/04 和 2014/06/15 之间并且时间在 12:00:00 和 20:00:00 之间的所有行。

日期没问题,因为我可以在 from_date 和 to_date 之间使用,但我的问题是时间。看表,时间并没有像日期一样增加,所以我不能在时间之间使用。例如,我的时间从 135911 开始,以 020719 结束,因此它只列出了这两个数字之间的行,但我需要所有蓝色行,例如不在此范围内的第 6 行。

【问题讨论】:

  • 日期时间列是什么类型的?
  • 您在查询中输入了哪些变量?
  • 日期是 int,时间是 varcahr。
  • 变量有fromDate、toDate、fromTime和toTime。
  • 两个时间变量的值是多少?

标签: php mysql sql select between


【解决方案1】:

这就是答案:
UTCdatedate 并且 satellite_derived_timetime 在表中。

//Get the accessible first day from date range between '$from_date' and '$to_date'. Then get the first time that is bigger or equal to '$from_time'. If there is no time bigger than '$from_time' in the specified first day so it get the first time of next day.
{
  //Find the first accessible day between two given date range.
  $first_day = "(SELECT UTCdate FROM $database_table WHERE UTCdate BETWEEN '{$from_date}' AND '{$to_date}' LIMIT 1)";
  $result = mysql_query($first_day) or die('Query faild'.mysql_error());
  $first_day_rows = mysql_fetch_assoc($result);


  //Get id of the nearest day that is biggest or equal to '$from_date' and '$from_time'.
  $first_time = "(SELECT id FROM $database_table WHERE UTCdate = '{$first_day_rows['UTCdate']}' AND satellite_derived_time >= CAST('{$from_time}' AS UNSIGNED) LIMIT 1)";
  $result = mysql_query($first_time) or die('Query faild'.mysql_error());
  $first_time_rows = mysql_fetch_assoc($result);


  if((!$first_time_rows) && $first_day_rows['UTCdate'])
  {
      $first_day = "(SELECT UTCdate FROM $database_table WHERE UTCdate > '{$first_day_rows['UTCdate']}' LIMIT 1)";

      $first_time = "(SELECT id FROM $database_table WHERE UTCdate = $first_day AND satellite_derived_time >= CAST('0' AS UNSIGNED) LIMIT 1)";
  }
}





//Get the accessible last day from date range between '$from_date' and '$to_date'. Then get the last time that is smaller or equal to '$to_time'. If there is no time lesser than '$to_time' in the specified last day so it get the last time of previous day.
{
  //Find the last accessible day between two given date range.
  $last_day = "(SELECT UTCdate FROM $database_table WHERE UTCdate BETWEEN '{$from_date}' AND '{$to_date}' ORDER BY UTCdate DESC LIMIT 1)";
  $result = mysql_query($last_day) or die('Query faild'.mysql_error());
  $last_day_rows = mysql_fetch_assoc($result);



  //Get id of the nearest day that is biggest or equal to '$from_date' and '$from_time'.
  $last_time = "(SELECT id FROM $database_table WHERE UTCdate = '{$last_day_rows['UTCdate']}' AND satellite_derived_time <= CAST('{$to_time}' AS UNSIGNED) ORDER BY satellite_derived_time DESC LIMIT 1)";
  $result = mysql_query($last_time) or die('Query faild'.mysql_error());
  $last_time_rows = mysql_fetch_assoc($result);


  if((!$last_time_rows) && $last_day_rows['UTCdate'])
  {
      $last_day = "(SELECT UTCdate FROM $database_table WHERE UTCdate < '{$last_day_rows['UTCdate']}' ORDER BY UTCdate DESC LIMIT 1)";          

      $last_time = "(SELECT id FROM $database_table WHERE UTCdate = $last_day AND satellite_derived_time <= CAST('235959' AS UNSIGNED) ORDER BY satellite_derived_time DESC LIMIT 1)";
  }
}

【讨论】:

    【解决方案2】:

    编辑 1

    这完全适合我:

    $query = "SELECT * FROM $database_table 
               WHERE UTCdate BETWEEN '{$from_date}' AND '{$to_date}' 
                 AND satellite_derived_time 
                       BETWEEN CAST('{$from_time}' AS UNSIGNED) 
                           AND CAST('{$to_time}' AS UNSIGNED)";
    

    但没有显示我需要的所有行。它只显示在12:00:0020:00:00 之间的行,例如不显示第6 行,因为它的时间是23

    将查询的一部分更改如下:

    SELECT * FROM $database_table 
     WHERE str_to_date( concat( UTCdate, ' ', satellite_derived_time ),
                        '%Y%m%d %H%i%s' )
     BETWEEN str_to_date( concat( '{$from_date}', ' ', '{$from_time}' ),
                          '%Y%m%d %H%i%s' )
         AND str_to_date( concat( '{$to_date}', ' ', '{$to_time}' ),
                          '%Y%m%d %H%i%s' )
    

    假设您输入的日期是yyyymmdd 格式,时间是H:i:s 格式。

    select * from $database_table 
     where str_to_date( UTCdate, '%Y%m%d' ) 
            between str_to_date( '$from_date', '%Y%m%d' ) 
                and str_to_date( '$to_date', '%Y%m%d' ) 
       and str_to_date( satellite_derived_time, '%H%i%s' )
            /*
               between str_to_date( '$fromTime', '%Y%m%d' ) 
                   and str_to_date( '$toTime', '%Y%m%d' )
            */
            -- or, if you use straight time format, then 
            between '$fromTime' and '$toTime'
    

    使用预处理语句绑定变量。

    【讨论】:

    • 谢谢,但不要给出结果。这完全适合我:$query = "SELECT * FROM $database_table WHERE UTCdate BETWEEN '{$from_date}' AND '{$to_date}' AND satellite_derived_time BETWEEN CAST('{$from_time}' AS UNSIGNED) AND CAST('{ $to_time}' 未签名)";但没有显示我需要的所有行。它只显示在 12:00:00 和 20:00:00 之间的行,例如不显示第 6 行,因为它的时间是 23。
    • startTime 是指from_date 上的那个吗?
    【解决方案3】:

    编写 php 代码,以便您的数据库处理此命令。

    SELECT * FROM yourtable 
    WHERE UTCdate BETWEEN  20140104 and 20140615   -- these are integers
    AND satellite_derived_time BETWEEN '120000' and '200000' -- these are strings
    

    【讨论】:

    • 第 6 行呢?它是'234459'。我需要所有蓝色行。
    • 您的问题是,时间必须在中午到晚上 8 点之间。第 6 行比晚上 11:45 晚 1 秒,不在该时间范围内。我错过了什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多