简体   繁体   English

使用子查询从不同表中选择计数

[英]Selecting Counts from Different Tables with a Subquery

I'm new to MySQL, and I'd like some help in setting up a MySQL query to pull some data from a few tables (~100,000 rows) in a particular output format.我是 MySQL 的新手,我需要一些帮助来设置 MySQL 查询以从特定输出格式的几个表(~100,000 行)中提取一些数据。

This problem involves three SQL tables:这个问题涉及三个SQL表:

allusers : This one contains user information. allusers :这个包含用户信息。 The columns of interest are userid and vip感兴趣的列是useridvip

table1 and table2 contain data, but they also have a userid column, which matches the userid column in allusers . table1table2包含数据,但它们也有一个userid列,它与allusers中的userid列匹配。

What I'd like to do:我想做什么:

I'd like to create a query which searches through allusers , finds the userid of those that are VIP, and then count the number of records in each of table1 and table2 grouped by the userid .我想创建一个查询,它搜索allusers ,找到那些 VIP 的userid ,然后计算table1table2中每个按userid分组的记录数。 So, my desired output is:所以,我想要的输出是:

  userid  | Count in Table1  | Count in Table2
    1     |        5         |         21
    5     |        16        |         31
    8     |        21        |         12

What I've done so far:到目前为止我所做的:

I've created this statement:我创建了这个声明:

SELECT userid, count(1) 
FROM table1 
WHERE userid IN  (SELECT userid FROM allusers WHERE vip IS NOT NULL)
GROUP BY userid

This gets me close to what I want.这让我接近我想要的。 But now, I want to add another column with the respective counts from table2但是现在,我想添加另一列,其中包含table2的相应计数

I also tried using joins like this:我也尝试使用这样的连接:

select A.userid, count(T1.userid), count(T2.userid) from allusers A
left join table1 T1 on T1.userid = A.userid
left join table2 T2 on T2.userid = A.userid
where A.vip is not null
group by A.userid

However, this query took a very long time and I had to kill the query.但是,这个查询花了很长时间,我不得不终止查询。 I'm assuming this is because using Joins for such large tables is very inefficient.我假设这是因为对如此大的表使用连接效率非常低。

Similar Questions类似问题

This one is looking for a similar result as I am, but doesn't need nearly as much filtering with subqueries 这个正在寻找与我类似的结果,但不需要使用子查询进行几乎那么多的过滤

This one sums up the counts across tables, while I need the counts separated into columns 这个总结了跨表的计数,而我需要将计数分成几列

Could someone help me set up the query to generate the data I need?有人可以帮我设置查询以生成我需要的数据吗?

Thanks!谢谢!

You need to pre-aggregate first, then join, otherwise the results will not be what you expect if a user has several rows in both table1 and table2 .您需要先预聚合,然后加入,否则如果用户在table1table2都有几行,结果将不会是您期望的。 Besides, pre-aggregation is usually more efficient than outer aggregation in a situation such as yours.此外,在您这种情况下,预聚合通常比外部聚合更有效。

Consider:考虑:

select a.userid, t1.cnt cnt1, t2.cnt cnt2
from allusers a
left join (select userid, count(*) cnt from table1 group by userid) t1
    on t1.userid = a.userid
left join (select userid, count(*) cnt from table2 group by userid) t2
    on t2.userid = a.userid
where a.vip is not null

This is a case where I would recommend correlated subqueries:在这种情况下,我会推荐相关子查询:

select a.userid, 
       (select count(*) from table1 t1 where t1.userid = a.userid) as cnt1,
       (select count(*) from table2 t2 where t2.userid = a.userid) as cnt2
from allusers a
where a.vip is not null;

The reason that I recommend this approach is because you are filtering the alllusers table.我推荐这种方法的原因是因为您正在过滤alllusers表。 That means that the pre-aggregation approach may be doing additional, unnecessary work.这意味着预聚合方法可能会做额外的、不必要的工作。

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

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