【发布时间】:2021-06-25 06:45:24
【问题描述】:
我有两个表:question 和 answer,它们都有 created_date 列,它定义了它们的创建日期。
question 表:
mysql> describe question;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| created_date | datetime | NO | | NULL | |
| user_id | int(11) | NO | MUL | NULL | |
+--------------+--------------+------+-----+---------+----------------+
answer 表:
mysql> describe answer;
+--------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| created_date | datetime | NO | | NULL | |
| question_id | int(11) | NO | MUL | NULL | |
| user_id | int(11) | NO | MUL | NULL | |
+--------------+------------+------+-----+---------+----------------+
我想要的是获取有关用户的一些统计信息,即用户每天(或在某个日期范围内)发布的问题和答案的数量。
示例:对于 id 为 1 的用户,请获取用户在过去 30 天内发布的问题和答案的数量(记录应按时间顺序排列)。
所需的输出如下所示:
+---------------------+-----------+---------+
| date | questions | answers |
+---------------------+-----------+---------+
| 2021-01-02 | 0 | 1 |
| 2021-01-03 | 5 | 5 |
| 2021-01-04 | 1 | 0 |
| 2021-01-05 | 1 | 0 |
| 2021-01-06 | 5 | 2 |
+---------------------+-----------+---------+
我对 SQL 查询有所了解,但我相信这种类型的查询需要某种我从未理解过的 JOIN,并尽我所能避免它。
到目前为止我的想法(对于 id 为 1 的用户):
SELECT q.created_date, COUNT(q.id)
FROM question q, answer a
WHERE q.id = a.question_id
AND q.user_id = 1
GROUP BY CAST(q.created_date AS DATE)
ORDER BY q.created_date ASC;
结果:
+---------------------+-------------+
| created_date | count(q.id) |
+---------------------+-------------+
| 2021-01-02 13:47:15 | 4 |
| 2021-02-09 13:24:52 | 1 |
| 2021-03-02 18:31:14 | 12 |
+---------------------+-------------+
答案表应该有类似的输出。
如何将输出合并在一起?
编辑:
ID 为 2 的用户发布的所有日期和问题数量:
mysql> select cast(q.created_date as date) only_date, count(*)
from question q
where q.user_id = 2
group by only_date;
+------------+----------+
| only_date | count(*) |
+------------+----------+
| 2021-01-02 | 1 |
| 2021-02-10 | 1 |
| 2021-02-14 | 1 |
| 2021-03-16 | 1 |
| 2021-03-26 | 3 |
| 2021-03-27 | 23 |
| 2021-03-28 | 5 |
+------------+----------+
所有日期以及 id 为 2 的用户发布了多少答案:
mysql> select cast(a.created_date as date) only_date, count(*)
from answer a
where a.user_id = 2
group by only_date;
+------------+----------+
| only_date | count(*) |
+------------+----------+
| 2021-02-08 | 2 |
| 2021-02-14 | 1 |
+------------+----------+
期望的输出是:
+------------+-----------+---------+
| only_date | questions | answers |
+------------+-----------+---------+
| 2021-01-02 | 1 | 0 |
| 2021-02-08 | 2 | 0 |
| 2021-02-10 | 1 | 0 |
| 2021-02-14 | 1 | 1 |
| 2021-03-16 | 1 | 0 |
| 2021-03-26 | 3 | 0 |
| 2021-03-27 | 23 | 0 |
| 2021-03-28 | 5 | 0 |
+------------+-----------+---------+
【问题讨论】:
-
嗨,@Akina。我实际上是想在没有时间部分的情况下编写它,所以我会使用 CAST(created_date as DATE) 之类的东西来截断日期。我实际上不需要时间部分,只需要日期。我更新了问题。谢谢!
标签: mysql sql join group-by count