SQL从一个表中获取所有列,以及从另一个表中通过外键关联的分组列的计数

时间:2018-10-10 05:46:59

标签: mysql sql join group-by

我有2个表“ projects_logs”和“ logs_attachments”。 “ logs_attachments”表通过外键“ project_log_id”与“ projects_logs”相关。每个project_id与多个“ project_log_id”相关,每个“ project_log_id”与多个“ log_attachment_id”相关。我想获取一个项目的所有日志的记录,每个日志都没有附件的数量。

projects_logs

+----------------+------------+---------+-----------------------------------------------+---------------------+---------------------+
| project_log_id | project_id |  event  |                     notes                     |     created_at      |     updated_at      |
+----------------+------------+---------+-----------------------------------------------+---------------------+---------------------+
|              5 |          3 | started | Aut et dolores voluptatem id culpa inventore. | 2018-10-05 07:10:46 | 2018-10-05 07:10:46 |
|              6 |          3 | on-hold | dolores voluptatem id culpa                   | 2018-10-05 07:10:46 | 2018-10-05 07:10:46 |
|            241 |          3 | resumed | Isdnu sdlw o dolores voluptatem id            | 2018-10-05 07:10:46 | 2018-10-05 07:10:46 |
+----------------+------------+---------+-----------------------------------------------+---------------------+---------------------+

logs_attachments

+-------------------+----------------+---------------------------+-----------+-----------+------------+---------------------+---------------------+
| log_attachment_id | project_log_id |         file_name         | file_type | attachment | file_size |     created_at      |     updated_at      |
+-------------------+----------------+---------------------------+-----------+-----------+------------+---------------------+---------------------+
|                24 |              5 | hic                       | rtf       | 5_biY     |       1212 | 2018-10-05 07:10:47 | 2018-10-05 07:10:47 |
|                23 |              5 | omnis                     | xls       | 5_3QC     |       5594 | 2018-10-05 07:10:47 | 2018-10-05 07:10:47 |
|                26 |              6 | quo                       | xlsx      | 6_kfS     |       2765 | 2018-10-05 07:10:47 | 2018-10-05 07:10:47 |
|                27 |              6 | consequuntur              | txt       | 6_BGd     |         94 | 2018-10-05 07:10:47 | 2018-10-05 07:10:47 |
|              1201 |            241 | SampleJPGImage_50kbmb.jpg | jpeg      | 241_xYP   |      51085 | 2018-10-09 12:34:19 | 2018-10-09 12:34:19 |
+-------------------+----------------+---------------------------+-----------+-----------+------------+---------------------+---------------------+

我想要的

+----------------+------------+---------+-----------------------------------------------+---------------------+---------------------+----------------------------------------+
| project_log_id | project_id | events  |                     notes                     |     created_at      |     updated_at      | count(logs_attachments.project_log_id) |
+----------------+------------+---------+-----------------------------------------------+---------------------+---------------------+----------------------------------------+
|              5 |          3 | started | Aut et dolores voluptatem id culpa inventore. | 2018-10-05 07:10:46 | 2018-10-05 07:10:46 |                                      2 |
|              6 |          3 | on-hold | dolores voluptatem id culpa                   | 2018-10-05 07:10:46 | 2018-10-05 07:10:46 |                                      2 |
|            241 |          3 | resumed | Isdnu sdlw o dolores voluptatem id            | 2018-10-05 07:10:46 | 2018-10-05 07:10:46 |                                      1 |
+----------------+------------+---------+-----------------------------------------------+---------------------+---------------------+----------------------------------------+

3 个答案:

答案 0 :(得分:2)

一个简单的连接应该在这里工作:

SELECT
    p.project_log_id,
    p.project_id,
    p.event,
    p.notes,
    p.created_at,
    p.updated_at
    COALESCE(a.count, 0) AS count
FROM projects_logs p
LEFT JOIN
(
    SELECT project_log_id, COUNT(*) AS count
    FROM logs_attachments a
    GROUP BY project_log_id
) a
    ON p.project_log_id = a.project_log_id;

答案 1 :(得分:1)

对子查询使用连接和count()

select t1.*,
COALESCE(t2.cnt, 0)  as count_logs_attachments.project_log_id
from 
projects_logs as t1
left join 
(
select project_log_id,count(*) as cnt from  logs_attachments
group by project_log_id
) t2 on 
t1.project_log_id=t2.project_log_id

COALESCE函数将帮助您在没有logs_attachments的情况下获得0

答案 2 :(得分:0)

您可以编写

之类的sql查询
select pl.project_log_id, pl.project_id, pl.event, pl.notes, pl.created_at, pl.updated_at, pla.attachment_count
from projects_logs as pl
inner join
    (select count(log_attachment_id) as attachment_count, project_log_id
     from  logs_attachments group by project_log_id) as pla
     on pla.project_log_id = pl.project_log_id