I have two tables: question and answer and both have the column created_date which defines the date they were created.
The question table:
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 | |
+--------------+--------------+------+-----+---------+----------------+
The answer table:
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 | |
+--------------+------------+------+-----+---------+----------------+
What I want is to get some statistics about a user, meaning the number of questions and answers the user posted each day (or from a range of dates).
Example: For user with id 1 get me the number of questions and answers the user posted for the last 30 days (the records should be ordered chronologically).
The desired output would look something like this:
+---------------------+-----------+---------+
| 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 |
+---------------------+-----------+---------+
I have knowledge on SQL queries, but I believe for this type of query requires some kind of JOIN which I've never understood and did my best to avoid it.
What I've come up so far (for a user with id of 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;
Which results in:
+---------------------+-------------+
| 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 |
+---------------------+-------------+
A similar output should go for the answer table.
How do I merge the outputs together?
EDIT:
All dates and how many questions the user with id of 2 posted:
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 |
+------------+----------+
All dates and how many answers the user with id of 2 posted:
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 |
+------------+----------+
The desired output would be:
+------------+-----------+---------+
| 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 |
+------------+-----------+---------+