简体   繁体   中英

How to count and display number of rows in database with the same date (PHP)

I have a database that contains three different columns: domain, date, length (in that order). What I'm trying to do is count the occurrence of every unique date in the database without knowing what the end date is in the database. The start date is always todays date.

So far I've only been able to count the occurences of a specific date, which requires me to put in an exact date.

What I want the PHP script to do is to start with todays date and output the number of times the date is mentioned (or the number of rows with that date) and then continue until it reaches a date that doesn't have a value (you could call this the end date).

I'm surprised and frustrated that I haven't been able to find the solution yet. It seems like a very easy thing to do.

I'd be super happy for any hints, tips or solutions.

结合使用GROUPWHERE子句。

SELECT COUNT(*) AS `occurrences`, `date` FROM `table` WHERE `date` >= CURDATE() GROUP BY `date`

Use group by and order by

SELECT dateCol, Count(*) FROM myTable 
 WHERE dateCol >= date(now())
 GROUP BY dateCol
 ORDER BY dateCol ASC

Edit: SQLFiddle with your example: http://www.sqlfiddle.com/#!2/5e86b/2

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