Select 多个表中的多个计数 (*) 具有单个查询
Select multiple count(*) in multiple tables with single query
我有 3 个 tables:
- 基本
id
姓名
描述
2
名字 1
描述2
3
名字2
描述3
- 链接A
id
linkA_ID
2
344
3
3221
2
6642
3
2312
2
323
- 链接B
id
linkB_ID
2
8287
3
42466
2
616422
3
531
2
2555
2
8592
3
1122
2
33345
我想获得如下 table 结果:
id
姓名
描述
linkA_count
linkB_count
2
名字 1
描述2
3
2
3
名字2
描述3
5
3
我的查询:
SELECT
a.id
,a.name
,a.description
,COUNT(b.linkA_ID) AS linkA_count
,COUNT(c.linkB_ID) AS linkb_count
FROM
basic a
JOIN linkA b on (a.id = b.id)
JOIN linkb c on (a.id = c.id)
GROUP BY
a.id
,a.name
,a.description
查询结果是 linkA 的计数始终与 linkB 相同
试试这个(使用子查询)
SELECT
basic.id
,basic.name
,basic.description
,(select Count(linkA_ID) from LinkA where LinkA.id=basic.id) as LinkACount
,(select Count(linkB_ID) from LinkB where LinkB.id=basic.id) as LinkBCount FROM basic
方法 2(尝试 CTE)
with a as(select id,Count(linkA_ID)LinkACount from LinkA group by id)
, b as (select id,Count(linkB_ID)LinkBCount from LinkB group by id)
select basic.id,a.LinkACount,b.linkBCount
from basic
join a on (a.id=basic.id)
join b on (b.id=basic.id)
一种更传统的方法是使用“派生表”(子查询),以便在联接乘以行之前执行计数。使用左连接允许查询返回 basic
中的所有 id,即使在两个连接表中都没有相关行也是如此。
select
basic.id
, coalesce(a.LinkACount,0) LinkACount
, coalesce(b.linkBCount,0) linkBCount
from basic
left join (
select id, Count(linkA_ID) LinkACount from LinkA group by id
) as a on a.id=basic.id
left join (
select id, Count(linkB_ID) LinkBCount from LinkB group by id
) as b on b.id=basic.id
如果您的 table 中只有 select,您就会明白为什么您的查询无法工作。
SELECT
*
FROM
basic a
JOIN linkA b on (a.id = b.id)
JOIN linkb c on (a.id = c.id)
WHERE a.ID = 3
=> 只需在计数中使用 distinct
SELECT
a.id
,a.name
,a.description
,COUNT(DISTINCT(b.linkA_ID)) AS linkA_count
,COUNT(DISTINCT(c.linkB_ID)) AS linkb_count
FROM
basic a
JOIN linkA b on (a.id = b.id)
JOIN linkb c on (a.id = c.id)
GROUP BY
a.id
,a.name
,a.description
我有 3 个 tables:
- 基本
id | 姓名 | 描述 |
---|---|---|
2 | 名字 1 | 描述2 |
3 | 名字2 | 描述3 |
- 链接A
id | linkA_ID |
---|---|
2 | 344 |
3 | 3221 |
2 | 6642 |
3 | 2312 |
2 | 323 |
- 链接B
id | linkB_ID |
---|---|
2 | 8287 |
3 | 42466 |
2 | 616422 |
3 | 531 |
2 | 2555 |
2 | 8592 |
3 | 1122 |
2 | 33345 |
我想获得如下 table 结果:
id | 姓名 | 描述 | linkA_count | linkB_count |
---|---|---|---|---|
2 | 名字 1 | 描述2 | 3 | 2 |
3 | 名字2 | 描述3 | 5 | 3 |
我的查询:
SELECT
a.id
,a.name
,a.description
,COUNT(b.linkA_ID) AS linkA_count
,COUNT(c.linkB_ID) AS linkb_count
FROM
basic a
JOIN linkA b on (a.id = b.id)
JOIN linkb c on (a.id = c.id)
GROUP BY
a.id
,a.name
,a.description
查询结果是 linkA 的计数始终与 linkB 相同
试试这个(使用子查询)
SELECT
basic.id
,basic.name
,basic.description
,(select Count(linkA_ID) from LinkA where LinkA.id=basic.id) as LinkACount
,(select Count(linkB_ID) from LinkB where LinkB.id=basic.id) as LinkBCount FROM basic
方法 2(尝试 CTE)
with a as(select id,Count(linkA_ID)LinkACount from LinkA group by id)
, b as (select id,Count(linkB_ID)LinkBCount from LinkB group by id)
select basic.id,a.LinkACount,b.linkBCount
from basic
join a on (a.id=basic.id)
join b on (b.id=basic.id)
一种更传统的方法是使用“派生表”(子查询),以便在联接乘以行之前执行计数。使用左连接允许查询返回 basic
中的所有 id,即使在两个连接表中都没有相关行也是如此。
select
basic.id
, coalesce(a.LinkACount,0) LinkACount
, coalesce(b.linkBCount,0) linkBCount
from basic
left join (
select id, Count(linkA_ID) LinkACount from LinkA group by id
) as a on a.id=basic.id
left join (
select id, Count(linkB_ID) LinkBCount from LinkB group by id
) as b on b.id=basic.id
如果您的 table 中只有 select,您就会明白为什么您的查询无法工作。
SELECT
*
FROM
basic a
JOIN linkA b on (a.id = b.id)
JOIN linkb c on (a.id = c.id)
WHERE a.ID = 3
=> 只需在计数中使用 distinct
SELECT
a.id
,a.name
,a.description
,COUNT(DISTINCT(b.linkA_ID)) AS linkA_count
,COUNT(DISTINCT(c.linkB_ID)) AS linkb_count
FROM
basic a
JOIN linkA b on (a.id = b.id)
JOIN linkb c on (a.id = c.id)
GROUP BY
a.id
,a.name
,a.description