简体   繁体   English

根据日期查询对SQL行进行计数

[英]Count SQL Rows based on Date Query

I'm building a really simple PHP app where they just want to list out their current orders (from an SQL database) and make a few calls based on when the due date of the orders are. 我正在构建一个非常简单的PHP应用程序,他们只想列出当前订单(从SQL数据库中),然后根据订单的到期日期进行一些调用。

I'm trying to count all the orders in the table that have due dates less than 6 days from now... and I am STRUGGLING! 我正在尝试计算表中所有到期日从现在开始少于6天的订单...而我正在挣扎!

I've got it displaying all the orders in a table, but I have no idea how to approach counting the rows based on their due date. 我已经在表中显示了所有订单,但是我不知道如何根据到期日对行进行计数。

The order_duedate is in date format. order_duedate为日期格式。

Does anyone have any suggestions?? 有没有人有什么建议??

I'm pretty new to pulling info from databases so this could be quite simple - Here is the code I am using: 我是从数据库中提取信息的新手,所以这可能非常简单-这是我正在使用的代码:

<?php

ob_start();
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'aaa');
define('DB_PASSWORD', 'aaa');
define('DB_DATABASE', 'aaa');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());

// TABLE ROWS AS FOLLOWS
// order_no / order_client / order_duedate / order_status


$table_all = "SELECT * FROM orders";
$table_all_data = mysql_query($table_all);


// COUNT ORDERS THAT HAVE ORDER_DUEDATE < 6 DAYS FROM NOW


?>

<p>TOTAL OVERDUE ORDERS</p>
<p><?php echo $total_overdue ?></p>



<?php
// DISPLAY COMPLETE TABLE OF DATA
while($row = mysql_fetch_array($table_all_data)){ 
    // CHANGE FORMAT OF DATE
    $reformat_duedate = $row['order_duedate'];
    $phpdate = strtotime( $reformat_duedate );
    $newdate = date( 'd/m/Y', $phpdate );

?>

<tr>
    <td><a href=""><?php echo $row['order_no'] ?></a></td>
    <td class="alignleft"><?php echo $row['order_client'] ?></td>
    <td><span class="date open"><?php echo $newdate ?></span></td>
    <td class="alignleft"><?php echo $row['order_status'] ?></td>
    <td>2</td>
    <td><a href=""><i class="fa fa-pencil" aria-hidden="true"></i></a>&nbsp;&nbsp;&nbsp;<a href=""><i class="fa fa-times" aria-hidden="true"></i></a></td>
</tr>


<?php } 

mysql_close(); ?>

Thank you so much! 非常感谢!

SELECT *
FROM orders
WHERE date_add(order_duedate, INTERVAL -6 DAY) < now()

您可以尝试SELECT * FROM orders WHERE order_duedate <= NOW() + INTERVAL 6 DAY

运行另一个查询:

select count(*) from orders where order_duedate < CURDATE() + INTERVAL 6 DAY;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM