我有一些问题要显示访问次数最多的日期。下面是数据库中的样子:
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'];提前谢谢。
发布于 2012-11-28 03:54:22
这将显示出总访问量相同的日期。
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
)发布于 2012-11-28 03:53:14
在您的DATE中,您缺少了围绕date_firstvisit的date_firstvisit函数。您还需要一个别名来使用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看到它的行动
https://stackoverflow.com/questions/13597470
复制相似问题