Get count of foreign key from multiple tables

前端 未结 3 999
滥情空心
滥情空心 2021-01-18 17:39

I have 3 tables, with Table B & C referencing Table A via Foreign Key. I want to write a query in PostgreSQL to get all ids from A and also their total occurrences from

3条回答
  •  心在旅途
    2021-01-18 18:16

    Another option:

    SELECT
        a.id,
        (SELECT COUNT(*) FROM b WHERE b.a_id = a.id) +
        (SELECT COUNT(*) FROM c WHERE c.a_id = a.id)
    FROM
        a
    

提交回复
热议问题