简体   繁体   English

如何获得相关计数或具有相关记录的两个不同表?

[英]How to get the related count or two different tables with related records?

I have an account table that has two related tables. 我有一个包含两个相关表的帐户表。 I'm trying to get an independent count of the related rows for those two tables but when I do the second join it changes the results from the first count. 我试图获得这两个表的相关行的独立计数,但是当我进行第二次连接时,它将更改第一个计数的结果。

account 帐户

account_service_a account_service_b account_service_a account_service_b

select a.id as account_id, aa.id as aa_id, ab.id as ab_id 
from account a 
inner join account_service_a aa on aa.account_id = a.id 
inner join account_service_b ab = ab.account_id = a.id;

+------------+---------+----------+
| account_id | aa_id   | ab_id    |
+------------+---------+----------+
|    7341383 | 3442287 | 20966936 |
|    7341383 | 3442287 | 24671972 |
|    7341383 | 3442287 | 31195473 |
|    7341383 | 3442287 | 31195658 |
|    7341383 | 3442287 | 31195730 |
|    7341383 | 3442287 | 31195798 |
|    7341383 | 3442287 | 31195925 |
|    7341383 | 3442287 | 31195966 |
|    7341383 | 3442287 | 31196022 |

So as you can see account 7341383 has one related aa record (3442287) and 9 unique ab records. 因此,您可以看到帐户7341383具有一个相关的aa记录(3442287)和9个唯一的ab记录。 I'd like to write a group by statement that gives me the count of those two tables' related records. 我想编写一个group by语句,该语句可以让我计算出这两个表的相关记录的数量。 A single group by works, but one that contains two joins ends up skewing the results. 单个by by分组,但是包含两个联接的分组最终会使结果倾斜。

select a.id as account_id, count(aa.id) as aa_count 
from account a 
inner join account_service_a aa on aa.account_id = a.id 
group by a.id

+------------+---------+
| account_id | aa_count| 
+------------+---------+
|    7341383 |    1    | 


select a.id as account_id, count(aa.id) as aa_count, count(ab.id) as ab_count 
from account a 
inner join account_service_a aa on aa.account_id = a.id 
inner join account_service_b ab = ab.account_id = a.id 
group by a.id;

+------------+---------+----------+
| account_id | aa_id   | ab_id    |
+------------+---------+----------+
|    7341383 |    9    |    9     |

How can I get the counts to operating independently? 如何获得独立运行的能力?

Use COUNT(DISTINCT) . 使用COUNT(DISTINCT)

select a.id as account_id, count(DISTINCT aa.id) as aa_count, count(*) as ab_count 
from account a 
inner join account_service_a aa on aa.account_id = a.id 
inner join account_service_b ab on ab.account_id = a.id 
group by a.id;

you can group by multiple columns so you have to add a group by aa.id too 您可以按多列分组,因此您也必须按aa.id添加分组

select a.id as account_id, count(aa.id) as aa_count, count(ab.id) as ab_count from account a inner join account_service_a aa on aa.account_id = a.id inner join account_service_b ab = ab.account_id = a.id group by a.id, aa.id; 选择a.id作为account_id,count(aa.id)作为aa_count,count(ab.id)作为ab_count从aa.account_id = a.id内部联接account_service_a aa的帐户a。内联接account_service_b ab = ab.account_id = a。 id由a.id,aa.id分组;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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