简体   繁体   中英

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.

the SQL query at the moment is like this.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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