两个日期之间的SQL查询

时间:2014-02-14 10:33:41

标签: php mysql sql date

form.php的

<form  action="dropdown.php" method="POST">
<?php
$month = array();
    for ( $i=1; $i<13; $i++ ) {
    $month = date('m', mktime(0,0,0,$i,2,2000));
    $sel = ( $i == date('n') ? ' selected="selected"' : '');
    $options1[] = "<option  value=\"{$month}\" {$sel}>{$month}</option>";
}
    $options_list1 = join("", $options1);
    echo "<select name=\"month\" >{$options_list1}</select>";
    for ( $j=1; $j<32; $j++ ) {
    $theday = date('d', mktime(0,0,0,0,$j,2000));
    $sel = ( $j == date('d') ? ' selected="selected"' : '');
    $options2[] = "<option  value=\"{$theday}\" {$sel}>{$theday}</option>";
}
    $options_list2 = join("\r\n", $options2);
    echo "<select name=\"day\" >{$options_list2}</select>";
    $arrivalyear = array(2013 => "2013",2014 => "2014");
    $selected = date("Y");
    echo '<select name="year">';
    foreach ($arrivalyear as $i => $v) {
    echo "<option  value=\"" . $i . "\"";
if ($i == $selected) echo " selected=\"selected\"";
    echo ">" . $v . "</option>";
    } 
    echo '</select>';
?>
<span>&nbsp;&nbsp;&nbsp;BETWEEN&nbsp;&nbsp;&nbsp;</span>
<?php
$month = array();
    for ( $i=1; $i<13; $i++ ) {
    $month = date('m', mktime(0,0,0,$i,2,2000));
    $sel = ( $i == date('n') ? ' selected="selected"' : '');
    $options1[] = "<option  value=\"{$month}\" {$sel}>{$month}</option>";
}
    $options_list1 = join("", $options1);
    echo "<select name=\"month1\" >{$options_list1}</select>";
    for ( $j=1; $j<32; $j++ ) {
    $theday = date('d', mktime(0,0,0,0,$j,2000));
    $sel = ( $j == date('d') ? ' selected="selected"' : '');
    $options2[] = "<option  value=\"{$theday}\" {$sel}>{$theday}</option>";
}
    $options_list2 = join("\r\n", $options2);
    echo "<select name=\"day1\" >{$options_list2}</select>";
    $arrivalyear = array(2013 => "2013",2014 => "2014");
    $selected = date("Y");
    echo '<select name="year1">';
    foreach ($arrivalyear as $i => $v) {
    echo "<option  value=\"" . $i . "\"";
if ($i == $selected) echo " selected=\"selected\"";
    echo ">" . $v . "</option>";
    } 
    echo '</select>';
?>
<input type="submit" name="sub" value="Date Filter">

</form>

如果用户选择2014-02-102014-02-08,则日期有两个选择选项。它将显示这两个日期之间的日期

我知道这个查询,但这不像我想要的那样工作,因为如果用户选择第一个日期然后第二个日期,那么我的查询将不会像我想要的那样出现

select * from mdx
  where mdx_timestamp >= '2014-02-10' and mdx_timestamp <= '2014-02-08'  

如果有任何解决方案可以找到两个日期之间的日期,而日期来自db

5 个答案:

答案 0 :(得分:1)

使用Between条款

select * from mdx where mdx_timestamp between '2014-02-08' and '2014-02-10'

答案 1 :(得分:0)

可能是您的查询应该像

select * from mdx where mdx_timestamp <= '2014-02-10' and mdx_timestamp >= '2014-02-08'

逻辑上没有大于2014-02-10且小于2014-02-08的日期。您需要反向执行

答案 2 :(得分:0)

使用between子句:

select * from mdx where mdx_timestamp between '2014-02-08' and '2014-02-10'

或者:

select * from mdx 
  where mdx_timestamp between 
              least( '2014-02-08', '2014-02-10' )
              and
              greatest( '2014-02-08', '2014-02-10' )

请参阅
MySQL: expr BETWEEN min AND max

答案 3 :(得分:0)

我相信您必须将时间添加到日期,否则它将无法正常工作:

select * from mdx where mdx_timestamp >= '2014-02-10 00:00:00' and mdx_timestamp <= '2014-02-08  23:59:59' 

答案 4 :(得分:0)

“SELECT * FROM mdx WHERE mdx_timestamp&gt; =”2014-02-08“AND mdx_timestamp&lt; =”2014-02-10“;