Php 如何在mysql中选择日期和时间之间的行?

Php 如何在mysql中选择日期和时间之间的行?,php,mysql,sql,select,between,Php,Mysql,Sql,Select,Between,有一个表有三列。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

有一个表有三列。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行。

编辑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:00到20: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格式


使用准备好的语句绑定变量。

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

SELECT * FROM yourtable 
WHERE UTCdate BETWEEN  20140104 and 20140615   -- these are integers
AND satellite_derived_time BETWEEN '120000' and '200000' -- these are strings
答案是: UTCdate是表中的日期,卫星时间是表中的时间

//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)";
  }
}

日期和时间列是什么类型?在查询中输入什么变量?日期是int,时间是varcahr。变量是fromDate、toDate、fromTime和toTime。这两个时间变量的值是什么?谢谢,但不要给出结果。这项工作对我来说非常有用:$query=SELECT*FROM$database_table,其中UTCdate介于{$FROM_date}和{$to_date}之间,satellite_派生的_time介于将{$FROM_time}转换为未签名和将{$to_time}转换为未签名之间;但并没有显示我需要的所有行。它只显示12:00:00到20:00:00之间的行,例如,不显示第6行,因为它的时间是23。所谓开始时间,是指从开始日期开始吗?第6行呢?是234459。我需要所有的蓝排。你的问题是时间必须在中午到晚上8点之间。第6排距离晚上11:45还有1秒,不在该时间范围内。我错过了什么?
//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)";
  }
}