[英]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.
我只想计算给定表中匹配的日期(不是小时/分钟/秒),然后使用php在简单的日历上突出显示给定的日期。 (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%.
我拉一些行,但不是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. 我希望该语句对以下查询显示:1月3日唯一条目。 February 2 matching entries.
2月2日匹配条目。 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. 当给定一天有一个,两个或三个条目时,我正在使用php更改日历的字体。 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)
: 您的子查询需要按
date(start)
分组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. 但是,从您的描述来看,子查询的输出似乎已足够,因为它可以标识具有多个条目的日期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.