简体   繁体   中英

Select statement that counts matching dates (not time) from a timestamp in sql

I don't want to combine or group them. I only want to count how many dates (not the hours/min/sec) match in a given table then use php to highlight the given days on a simple calendar. (Thus highlighting days that have more than two entries.)

I just can't figure out the statement. Here's an example of what I've tried. I pulls SOME rows, but it's not 100%.

    SELECT * 
    FROM `foo_table` 
    WHERE DAY(`start`) IN
    (
        SELECT DAY(`start`)         
        FROM `foo_table`
        WHERE MONTH(`start`) IN (01) 
        AND MONTH(`end`) IN (01) 
        GROUP BY `start` 
        HAVING COUNT(*) > 1 
    )
    ORDER BY `start`

Here's the table:

    id          start                      end
    1    2014-01-01 10:00:00       2014-01-02 10:00:00
    2    2014-01-02 10:00:00       2014-01-04 10:00:00
    3    2014-01-03 10:00:00       2014-01-06 10:00:00
    4    2014-02-01 10:00:00       2014-02-02 10:00:00
    5    2014-02-01 10:00:00       2014-02-02 10:00:00
    6    2014-10-01 10:00:00       2014-10-19 10:00:00

I want the statement to show for a query of: January 3 unique entries. February 2 matching entries. And so on.

I'm using php to change the font of a calendar when there is one, two, or three entries for a given day. I just can't seem to figure out how to find out the amount of entries and which days.

If I'm understanding you correctly, would something like this work for you:

SELECT DAY(`start`), COUNT(*) 
FROM foo_table 
WHERE DATE_FORMAT(`start`, "%Y-%m") = '2014-03' 
GROUP BY 1;

This would return you something similar to:

+-----------+----------+
|         1 |      266 |
|         2 |      260 |
|         3 |      304 |
|         4 |      298 |
|         5 |      251 |
|         6 |      293 |
|         7 |      310 |
|         8 |      243 |
|         9 |      248 |
|        10 |      288 |
|        11 |      290 |
|        12 |      245 |
|        13 |      315 |
|        14 |      333 |
|        15 |      298 |
|        16 |      268 |
|        17 |      282 |
|        18 |      245 |
|        19 |      252 |
|        20 |      293 |
|        21 |       63 |
+-----------+----------+

Your subquery needs to group by date(start) :

SELECT * 
FROM `foo_table` 
WHERE DATE(`start`) IN (SELECT DATE(`start`)         
                        FROM `foo_table`
                        WHERE MONTH(`start`) IN (01) AND MONTH(`end`) IN (01) 
                        GROUP BY DATE(`start`)
--------------------------------^
                        HAVING COUNT(*) > 1 
                      )
ORDER BY `start`;

From your description, though, it would seem that the output of the subquery would be sufficient because it identifies the days with multiple entries.

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