3

我有一些问题要显示访问次数最多的日期。这是它在数据库中的样子:

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 visitorsSELECT * 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'];

提前致谢。

4

2 回答 2

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
)
于 2012-11-28T03:54:22.163 回答
3

您缺少. DATE_ 您还需要一个别名来处理函数的使用。date_firstvisitSELECTPDO

假设 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

看到它在行动

于 2012-11-28T03:53:14.017 回答