简体   繁体   English

将列添加到一个表中另一个是COUNT个

[英]Add Column to one Table that is COUNT of another

I have a list of subscribers in table Subscribers . 我在表Subscribers有一个订户列表。 Every time they receive a copy of their subscription, a new record is created in Subscriptions_Fulfilments for each Subscribers.ID . 每次他们收到订阅的副本时,都会在Subscriptions_Fulfilments为每个Subscribers.ID创建一个新记录。

I can create a table showing each Subscriber ID and the number of copies they received with the following query: 我可以创建一个表,其中显示每个订户ID以及通过以下查询获得的副本数:

SELECT Sub_ID, COUNT(Sub_ID) fcount FROM `Subscriptions_Fulfilments` 
GROUP BY Sub_ID

But I need to create a compound query that returns Subscribers along with a column showing the COUNT(Sub_ID) of Subscriptions_Fulfilments . 但是我需要创建一个复合查询,该查询返回Subscribers以及一列显示Subscriptions_Fulfilments的COUNT(Sub_ID)的列。

So I have two questions: 所以我有两个问题:

A) How would you make a query to create a table that shows each Subscriber and the number of times they've received their subscription, based on the COUNT of that Subscriber's ID in Subscriptions_Fulfilments ? A)您将如何查询以创建一个表,该表根据Subscriptions_Fulfilments中该订阅者ID的数量显示每个订阅者及其接收订阅的次数?

B) I'm operating under the assumption that a single MySql query accomplishing this would be more efficient than, say, running two queries, the one above and a SELECT * FROM Subscriptions , and combining the resulting arrays in PHP. B)我的假设是,完成此操作的单个MySql查询要比运行两个查询(上面的查询和SELECT * FROM Subscriptions )并在PHP中合并结果数组效率更高。 I have a feeling I know the answer but I'd like to positively learn something new today. 我有一个知道答案的感觉,但我想今天积极学习一些新知识。

Unfortunately, after too many tries, I'm clearly not good enough at queries for this and I have very little past the above query to show for it. 不幸的是,经过太多的尝试,我显然对查询的能力不够强,过去的查询也很少显示出来。 I apologize if this ends up being a dup, I searched long and hard before asking, but it's quite difficult to search precisely for Query help... 我很抱歉,如果这最终是一个重复,我在询问之前经过了漫长而艰苦的搜索,但是要精确地搜索Query help却非常困难...

One of the big advantages of a relational database is the ability to do joins and combinations of the data in your tables in a way that allows for this functionality without having to actually store it in a separate table. 关系数据库的一大优点是能够以允许该功能的方式进行表中数据的联接和组合,而不必将其实际存储在单独的表中。

You can accomplish this with a subquery like this: 您可以使用以下子查询来完成此操作:

SELECT Subscribers.name, fulfilments.count FROM Subscribers
INNER JOIN (
     SELECT id, count(*) as count FROM Subscriptions_Fulfilments
     GROUP BY Sub_Id
 )fulfilments ON subscribers.id = fulfilments.id

This might not be 100% what you're looking for and I might have messed up your names, but hopefully this will start to get you in the neighborhood of being correct? 这可能不是您要找的100%,而且我可能弄错了您的名字,但希望这会开始使您正确。

Here is a simple example showing the Subscribers ID and the no of subscription they have received. 这是一个简单的示例,显示订户ID和已收到的订户编号。 Hope it helps. 希望能帮助到你。

Step 1: select the ids from the Subscriber table 步骤1:从“订户”表中选择ID
Step 2: select the no of counts of subscriptions received by each subscriber. 步骤2:选择每个订阅者收到的订阅数。
Step 3: Join both the table ON the basis of ID. 步骤3:根据ID将两个表都加入。

SELECT SubId, noSub FROM 
Subscribers sb JOIN (SELECT SubId AS sid, COUNT(*)AS noSub FROM Subscriptions_Fulfilments GROUP BY SubId)AS ss ON sb.SubId = ss.sid

Simply try execute this query: 只需尝试执行以下查询:

Select distinct Sub_ID, count from (SELECT Sub_ID, COUNT(Sub_ID) fcount FROM Subscriptions_Fulfilments GROUP BY Sub_ID); 选择不同的Sub_ID,从(SELECT Sub_ID,COUNT(Sub_ID)fcount FROM Subscriptions_Fulfilments GROUP BY Sub_ID)开始计数;

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

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