简体   繁体   中英

MySQL Query: Group by same column name from two different tables

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 |
+------------+-----------+---------+

A query like this should help. This is not a working query. Just a psuedo. Please see. Will post a working query if possible.

select
    t1.user_id, t1.date, t1.q_count, t2.ans_count
from
    (
        select
        user_id, date, count(questions) q_count
        from
        question
        group by
        user_id, date
    ) t1
    left outer join
    (
        select
        user_id, date, count(questions) ans_count
        from
        question
        group by
        user_id, date
    ) t2 on t1.user_id = t2.user_id and t1.date = t2.date
SELECT CAST(q.created_date AS DATE) created_date, 
       COUNT(DISTINCT q.id) questions,    -- only unique values are counted
       COUNT(a.id) answers     -- if multiple answers for the same question
                               -- are possible, add DISTINCT too
FROM question q
JOIN answer a ON q.id = a.question_id 
WHERE q.user_id = 1 
GROUP BY created_date       -- output column, not source table column, is used
ORDER BY created_date ASC;

U didnt tell us about the relation u have created between the tables which is very important. so i suggest u build Referential Integrity between the tables then it would be easy to generate report from both the tables...This is demo how u can create such relation

CREATE TABLE t1 

( cid int NOT Null, name Varchar(30), index(cid), PRIMARY KEY(cid) )TYPE=INNODB

CREATE TABLE t2 ( tid int NOT Null, amount int,

cid int NOT Null,
PRIMARY KEY(tid),
index(cid),
FOREIGN KEY (cid) References t1(cid)

)

By doing this u can link these tables with the same ID...

SELECT q.created_date, COUNT(q.id) 
FROM question q, answer a 
INNER JOIN question ON a.question_id = q.id
WHERE q.user_id = 1 
GROUP BY CAST(q.created_date AS DATE) 
ORDER BY q.created_date ASC;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM