简体   繁体   English

按年和月获取数据库中的记录

[英]Get records in database by year and month

What I have here is a dropdown's that selects records from database by year and month. 我这里是一个下拉菜单,可以按年份和月份从数据库中选择记录。 This works correctly getting year and only 1 month. 这可以正常工作一年,只有1个月。 What I need is get all Januanry to June records or July to December by year. 我需要的是按年获取所有Januanry到6月的记录或7月到12月的记录。 Help? 救命?

My date format is yyyy-mm-dd 我的日期格式是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");

If you want to fetch records for a specific month and year, use month and year functions on the date field. 如果要获取特定月份和年份的记录,请在日期字段上使用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
     )

And the statement 和声明

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

is equivalent to 相当于

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

And hence you should not use both between conditions. 因此,您不应在条件之间同时使用两者。
Instead change the lower and upper boundary values of between clause as desired. 而是根据需要更改between子句的上下边界值。

Example 1 : 范例1
If you want records between april and august , both inclusive, try 如果您希望记录在aprilaugust april之间(包括两者之间),请尝试

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

Example 2 : 范例2
If you want records within a specific month, say, 'october', try 如果要在特定月份内记录,例如“十月”,请尝试

Month( po_date ) = 10    -- for OCT

If you still want to use between for a single month output, try 如果您仍然想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