0

我一直在寻找关于 SO 的答案,但我没有解决我的问题。我有两张桌子,profiles存储profiles_visitors第一张的访客。

我想知道用户在过去 15 天内有多少访问者,所以我写了这个:

SELECT COUNT(pv.id) AS `count`, DATE(FROM_UNIXTIME(pv.created_at)) AS `date`
FROM profiles p
LEFT JOIN profiles_visitors pv ON pv.profiles_id=p.id
WHERE p.users_id='50' AND pv.created_at >= (CURDATE() - INTERVAL 15 day)
GROUP BY DATE(FROM_UNIXTIME(pv.created_at))

查询结果:

+-------+-------------------+
| count | date              |
+-------+-------------------+
| 2     | 2012-10-26        |
+-------+-------------------+
| 3     | 2012-10-29        |
+-------+-------------------+

期望的结果:

+-------+-------------------+
| count | date              |
+-------+-------------------+
| 0     | 2012-10-15        |
+-------+-------------------+
.......... 0s here ..........
+-------+-------------------+
| 0     | 2012-10-25        |
+-------+-------------------+
| 2     | 2012-10-26        |
+-------+-------------------+
| 0     | 2012-10-27        |
+-------+-------------------+
| 0     | 2012-10-28        |
+-------+-------------------+
| 3     | 2012-10-29        |
+-------+-------------------+
| 0     | 2012-10-30        |
+-------+-------------------+

先感谢您。

4

1 回答 1

0

使用profiles行数而不是profile_vistors(因为如果没有访问,则 profile_vistors 表中没有行)作为:

 SELECT COUNT(p.id) AS 'count'

此外,如果您愿意,您可以使用DATEDIFF以下功能:

    DATEDIFF(CURDATE(), pv.created_at) <=15
于 2012-10-30T12:48:55.767 回答