简体   繁体   中英

PHP mysql - how to sum rows based on grouped datetime field

id | date              | status |
---+-------------------+--------+
1  |2013-12-26 00:00:00|  sent  |
---+-------------------+--------+    
2  |2013-12-26 00:00:00|  sent  |
---+-------------------+--------+ 
3  |2013-12-26 00:00:00|  sent  |
---+-------------------+--------+ 
4  |2018-10-21 00:00:00|  sent  |
---+-------------------+--------+
5  |2018-10-21 00:00:00|  sent  |
---+-------------------+--------+
6  |2018-10-21 00:00:00|  sent  |
---+-------------------+--------+

I want to sum the 'sent' status based on the same 'date' rows, how to do that? so the result will be like:

id | date              | total_sent_status|
---+-------------------+------------------+
1  |2013-12-26 00:00:00|         3        |
---+-------------------+------------------+
2  |2018-10-21 00:00:00|         3        |
---+-------------------+------------------+

Thank you in advance

UPDATE

and the PHP part is:

<?php echo $stat->sent_time ?>

it will return: 2018-10-21 00:00:00 since it's a datetime record, I want to get only the Date part: 2018-10-21

is there anybody know? the PHP manual is confusing newbie like me :(

you can use this

SELECT id,date,count(status) FROM test WHERE status='sent' group by date

尝试:

SELECT id, date, SUM(id) AS total_sent_status FROM table WHERE status = 'sent' GROUP BY date;

try this query

SELECT id,date,sum(status) as total_sent_status 
FROM author where status = 'sent'       
GROUP BY date; 

试试这个查询:

SELECT id, DATE(`date`) date, Count(status) as total_sent_status FROM `table` WHERE `status`='sent' GROUP BY date;

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