简体   繁体   中英

Select, count and display (by group?)

I have a MySQL Table that looks like this (id/date time):

1 | 2014-08-12 00:23:15
2 | 2013-04-02 05:13:45
3 | 2014-08-12 06:12:18
4 | 2012-11-01 02:21:04

How would I do to retrieve each unique date (3 here) and count how many elements I have for each date using PHP and MySQL?

For instance, here I would like to get and display this info:

2012-11-01: 1 item
2013-04-02: 1 item
2014-08-12: 2 items

I believe I would have to use "GROUP BY date" but whatever I do, I can't tell how many items I have for each date.

Use COUNT() as Fred said with GROUP BY (as you rightly believed):

SELECT 
    DATE(date_time_col), 
    COUNT(*) as cnt
FROM
    your_table
GROUP BY
    DATE(date_time_col)

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