简体   繁体   English

如何在一个查询中根据另一个表中的 id 计算两个不同表中的值?

[英]How do I count values from two different tables based off a id from another table in one query?

I'm looking to count the amount of users and channel_memberships that have a channel_partner_id for each channel from the channel_partners table.我希望从channel_partners表中计算每个频道都有channel_partner_iduserschannel_memberships的数量。

I want to run these two queries in one query:我想在一个查询中运行这两个查询:

SELECT channel_partners.id,
    channel_partners.name,
    COUNT(channel_memberships.channel_partner_id) AS membership_count
    
FROM channel_partners
JOIN channel_memberships ON channel_partners.id = channel_memberships.channel_partner_id

GROUP BY channel_partners.id 

and

SELECT channel_partners.id,
    channel_partners.name,
    COUNT(users.channel_partner_id) AS user_count
    
FROM channel_partners
JOIN users ON channel_partners.id = users.channel_partner_id

GROUP BY channel_partners.id

I've tried to do this:我试过这样做:

SELECT channel_partners.id,
    channel_partners.name,
    COUNT(users.channel_partner_id) AS user_count,
    COUNT(channel_memberships.channel_partner_id) AS membership_count
    
FROM channel_partners
JOIN users ON channel_partners.id = users.channel_partner_id 
JOIN channel_memberships ON channel_partners.id = channel_memberships.channel_partner_id


GROUP BY channel_partners.id

When I run this both user_count and membership_count are equal and have values that are vastly different from when I run the two queries individually.当我运行这个时, user_countmembership_count是相等的,并且具有与我单独运行这两个查询时大不相同的值。 For example when ran individually, for the channel_partner with id 20 I get 1178 user_count, 12588 membership_count, and when I run both together I get user_count and membership_count equaling 14828664 for the channel_partner with id 20.例如,当单独运行时,对于 id 为 20 的 channel_partner,我得到 1178 user_count,12588 members_count,当我一起运行时,对于 id 为 20 的 channel_partner,我得到 user_count 和 member_count 等于 14828664。

Thanks for Martin and Nick in the comments for helping me solve this.感谢 Martin 和 Nick 在评论中帮助我解决这个问题。 I can use subqueries instead of joins.我可以使用子查询而不是连接。

This is the working query here:这是这里的工作查询:

SELECT channel_partners.id,
    channel_partners.name,
    (SELECT COUNT(users.channel_partner_id)
        FROM users
        WHERE users.channel_partner_id = channel_partners.id) AS user_count,
    (SELECT COUNT(channel_memberships.channel_partner_id)
        FROM channel_memberships
        WHERE channel_memberships.channel_partner_id = channel_partners.id) AS membership_count
    
FROM channel_partners

暂无
暂无

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

相关问题 从一个表中选择计数,然后根据一个ID加入另一个表 - Select count from one table and join another table based off of one id MySQL查询根据来自两个不同表的值选择一个表。 - MySQL query to select one table based on values from two different tables. 如何根据另外两个表的过滤器快速返回和计算一个表中的行数 - How to fast return and count rows from one table based on filter by two another tables 如何从两个不同的表中进行选择和计数,以在一个查询中获得帖子的“喜欢计数”? - How do I SELECT and COUNT from two diferent tables to get the “like count” of a post in one query? 如何使用 id 将两个表从一张表更新到另一张表? - How to update two tables using id from one table to another? 如何从两个表中查询匹配表中的外键和另一个表中的主键 - How do I query from two tables matching the foreign key from table with the primary key from another 通过一个查询根据ID从不同的表中选择不同的值 - Select Different values from different tables based on id's with one query MySQL 查询基于一个共同值从两个表中求和一列并计算另一列? - MySQL query to sum one column and count another column, from two tables, based on a common value? 插入两个表(相同的数据库)中,如何从一个表中获得唯一的ID? - Inserting into two tables (same DB) how do I get the unique ID from one table? MySQL:如何根据一个表中另一个表的ID从一个表中获取一条信息 - MySQL: How do I get a piece of information from one table based on it's id in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM