簡體   English   中英

按年和月獲取數據庫中的記錄

[英]Get records in database by year and month

我這里是一個下拉菜單,可以按年份和月份從數據庫中選擇記錄。 這可以正常工作一年,只有1個月。 我需要的是按年獲取所有Januanry到6月的記錄或7月到12月的記錄。 救命?

我的日期格式是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");

如果要獲取特定月份和年份的記錄,請在日期字段上使用monthyear功能。

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 april之間(包括兩者之間),請嘗試

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

范例2
如果要在特定月份內記錄,例如“十月”,請嘗試

Month( po_date ) = 10    -- for OCT

如果您仍然想between一個月的輸出between使用,請嘗試

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM