简体   繁体   English

来自其他两个表的表中相关字段的MySQL计数

[英]MySQL Count of related fields in a table from two other tables

I have four tables (fabrics, reservations and fabricmembers, nodes). 我有四个表(结构,保留和结构成员,节点)。

Each "node" may have have zero of more "fabricmembers" (associated with it). 每个“节点”可能有零个(与之关联的)更多“成员”。 Each "fabric" may have zero or more "fabricmembers" (associated with it). 每个“结构”可以具有零个或多个“结构成员”(与其关联)。 A node may have a "reservation" on it, associated it with a single "node". 一个节点上可能有一个“保留”,并与一个“节点”相关联。

Thus you can see a references between these things when: 因此,您可以在以下情况下看到这些事物之间的引用:

fabricmember.ipaddr = reservation.ipaddr

and

fabricmember.fabric = fabric.fabricname fabricmember.fabric = fabric.fabricname

(You can do all this without actually refering to the "node"). (您可以执行所有这些操作而无需实际引用“节点”)。

I want to run a single query that can show me for each FABRIC, how many (sum) RESERVATIONS are associated with it, and how many (sum) fabricmembers it has. 我想运行一个查询,该查询可以向我显示每个FABRIC,与之关联的预订总数(总数)以及其拥有的fabricmember总数(总数)。 Again, in a single query 同样,在单个查询中

I have been using the following query: 我一直在使用以下查询:

select 
   fabricmembers.fabric,
   count(reservations.ipaddr) as Memebers,
   count(nodes.ipaddr) as Reservations 
from fabricmembers  
LEFT JOIN (reservations,nodes) 
ON ((reservations.ipaddr = fabricmembers.ipaddr) and (nodes.ipaddr = fabricmembers.ipaddr))   
GROUP BY (fabricmembers.fabric);

And this almost works, however if a fabric has zero members, or it has members but those members have ZERO reservations, the fabric simply does not show up in the query. 几乎可以正常工作,但是,如果结构的成员为零 ,或者结构的成员为零但这些成员具有零保留,则该结构不会显示在查询中。

The following works to show me how many members a fabric has, even if that number is zero: 以下工作向我展示了结构具有多少个成员,即使该数目为零也是如此:

select fabricname,count(fabricmembers.ipaddr) 
from fabrics 
LEFT JOIN (fabricmembers)
 ON (fabrics.fabricname = fabricmembers.fabric) 
GROUP BY (fabrics.fabricname);

however, I can't figure out how to have the query also tell me the number of members. 但是,我不知道如何让查询也告诉我成员数。

Does any of this make sense? 这有道理吗? Any help would be appreciated! 任何帮助,将不胜感激!

Your query does not return fabrics with zero reservations / zero members, because you are trying to build it from tables where only fabrics with reservations / members exist! 您的查询不会返回保留为零/成员为零的结构,因为您正试图从仅存在保留/成员的结构的表中进行构建! The only place you can find the empty fabrics is the fabrics table - thus you should build it from it downwards: 可以找到空织物的唯一地方是织物表-因此,您应该从下往下构建它:

SELECT fabrics.fabricname, count(reservations.ipaddr), count(fabricmember.ipaddr)
FROM fabrics
LEFT JOIN fabricmembers ON fabrics.fabricname = fabricmembers.fabric
LEFT JOIN reservations ON fabricmembers.ipaddr = reservations.ipaddr
GROUP by fabric.fabricname

You need to use correlated sub-queries for this something like: 您需要使用相关子查询,例如:

SELECT fabricmembers.fabric f, 
       (SELECT count(*) FROM reservations r where r.ipaddr = f.ipaddr) Members, 
       (SELECT count(*) FROM nodes n where n.ipaddr = f.ipaddr) Reservations
FROM fabricmembers

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

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