简体   繁体   English

PHP / MySQL查询按日期范围求和的列

[英]PHP/MySQL query to Sum Columns by Date Range

I have a DB Table with the following columns. 我有一个包含以下各列的数据库表。

item_id | item_name | qty_sold | qty_left | date | employee_name | employee_ID |

I want to be able to have two fields, From (date) & To (date) to be able to query this using a PHP script and get the results from the date range and SUM the qty_sold and qty_left based on that date range while grouping the output by employee name and/or employee ID. 我希望能够有两个字段,From(日期)和To(日期),以便能够使用PHP脚本进行查询,并从日期范围中获取结果,并在分组时根据该日期范围求和qty_sold和qty_left按员工姓名和/或员工ID输出。

the SQL query at the moment is like this. 目前的SQL查询就是这样。

SELECT
date,
item_id,
item_name,
qty_sold,
qty_left,
employee_name,
employee_id
FROM daily_sells
GROUP BY date, employee_id
ORDER BY date

I think it would be like grouping the results by the date range but I'm not too certain how to go about this. 我认为这就像按日期范围对结果进行分组,但我不太确定该如何进行。

If more details are needed, please ask as I'm not certain what more information to provide at the moment. 如果需要更多详细信息,请询问,因为我不确定当前要提供什么更多信息。

Thanks in advance, 提前致谢,

You can group on arbitrary expressions, eg 您可以对任意表达式进行分组,例如

GROUP BY date BETWEEN 'yyyy-mm-dd' AND 'yyyy-mm-dd'
GROUP BY year(date), month(date)
GROUP BY true   // not very useful, but still possible.

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

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