2014-01-30 157 views
1

我在這裏是一個下拉菜單,按年份和月份從數據庫中選擇記錄。這工作正確得到年,只有1個月。我需要的是將Januanry的所有內容按年份分配到六月份的記錄或七月至十二月。幫幫我?如果你想爲一個特定的月和年取的記錄,對日期字段使用monthyear功能在年份和月份中獲取數據庫中的記錄

我的日期格式爲YYYY-MM-DD

<form id="form1" name="form1" action="<?php $_SERVER['PHP_SELF']?>" method="post"> 
<?php 
$startYear = 2013; 
$curYear = date('Y'); 
    echo'<select name="year">'; 
    foreach (range($startYear, $curYear) as $year) { 
    echo '<option>'.$year.'</option>'; 
    } 
    echo'</select>'; 

    echo'<select name="month">'; 
    for ($x=1; $x<=12; $x++) 
    { 
    $val=strlen($x); 
    if($val==1) 
    { 
    echo '<option value="'.'0'.$x.'">'.'0'.$x.'</option>'; 
    } else { 
    echo '<option value="'.$x.'">'.$x.'</option>'; 
    } 
    } 
    echo'</select>'; 
?> 
<input type="submit" name="date" value="date"/> 
</form> 

<?php 
    $year = $mysqli->real_escape_string($_POST["year"]); 
    $month = $mysqli->real_escape_string($_POST["month"]); 

    echo $year.'-'.$month; 
    $result1 = $mysqli->query("SELECT po_date, rfq, shop, nego, shop, po_number, counter FROM purchase_order WHERE po_date LIKE '$year-$month-__' GROUP BY counter ORDER BY po_date"); 
+0

「po_date」字段的類型是什麼? –

+0

[MySQL選擇日期範圍問題]的可能的重複(http://stackoverflow.com/questions/4897133/mysql-select-date-range-issue) –

+0

@FezVrasta類型是日期 – user3097736

回答

1

WHERE Year(po_date) = $year 
and (Month(po_date) between 1 and 6 -- for JAN to JUN 
     OR 
     Month(po_date) between 7 and 12 -- for JUL to DEC 
    ) 

和聲明

Month(po_date) between 1 and 6 -- for JAN to JUN 
OR 
Month(po_date) between 7 and 12 -- for JUL to DEC 

相當於

Month(po_date) between 1 and 12 -- for JAN to DEC 

,因此你不應條件之間同時使用。
改爲根據需要更改between子句的下限和上限值。

例1
如果你想aprilaugust之間,包括兩端的記錄,嘗試

Month(po_date) between 4 and 8 -- for APR to AUG, both inclusive 

例2
如果你想有一個特定的一個月內記錄,比如「十月',試試

Month(po_date) = 10 -- for OCT 

如果你仍然想使用between單月輸出,請嘗試

Month(po_date) between 10 and 10 -- for OCT 
+0

它不會影響我的日期格式?我正在使用mysql – user3097736

+0

我的答案也是在MySQL中。它不會影響你的數據,因爲它只是一個'select'。 –

+0

請不要用你的答案來要求澄清,它可能會很混亂。這就是評論的目的。 – vascowhite

相關問題