我有一些问题可以显示访问日期最多的日期。以下是数据库中的样子:
date_firstvisit
2012-11-25 15:49:16
2012-11-21 17:42:40
2012-11-21 15:36:04
2012-11-20 07:39:11
2012-11-19 09:19:36
2012-11-15 23:03:24
2012-11-15 22:57:47
2012-11-15 03:10:20
2012-11-15 03:10:14
此SQL查询应打印2012-11-15
,但只打印0000-00-00
:
SELECT date_firstvisit FROM visitors
GROUP BY DATE(date_firstvisit)
ORDER BY COUNT(date_firstvisit) DESC
LIMIT 1
如果我将SELECT date_firstvisit FROM visitors
替换为SELECT * FROM visitors
,则会打印2012-11-28
。这当然是错的!
我不知道我的SQL查询有什么问题,所以我现在问你,我错过了什么吗?
修改
此代码显示了我如何将数据打印到我的网站。根据njk,这个SQL是正确的,但它只是继续打印0000-00-00
。
$get_mostactive_date = $sql->query("SELECT DATE(date_firstvisit), COUNT(date_firstvisit) FROM visitors GROUP BY DATE(date_firstvisit) ORDER BY COUNT(date_firstvisit) DESC LIMIT 1");
$mostactive_date = $get_mostactive_date->fetch(PDO::FETCH_ASSOC);
echo $mostactive_date['date_lastactive'];
提前致谢。
答案 0 :(得分:5)
这将显示总访问次数相同的日期
SELECT DATE(date_firstvisit) Dates, COUNT(*) totalCount
FROM visitors
GROUP BY DATE(date_firstvisit)
HAVING COUNT(*) =
(
SELECT MAX(s)
FROM(
SELECT COUNT(*) s
FROM visitors
GROUP BY DATE(date_firstvisit))s
)
答案 1 :(得分:3)
您错过了DATE
中date_firstvisit
周围的SELECT
功能。您还需要一个别名来处理PDO
函数的使用。
假设MySQL:
SELECT DATE(date_firstvisit) AS firstvisit, COUNT(date_firstvisit) AS count
FROM visitors
GROUP BY DATE(date_firstvisit)
ORDER BY COUNT(date_firstvisit) DESC
LIMIT 1