以下是我的表格:
我想计算D表中所有条目,其中“open”= true,哪个具有相同A.id和B.id的C.id
我希望查询在一行中返回计数,公共A.id和公共B.id
例如 D表
D.id = 1, open=true, D.CID = 2
D.id = 2, open=true, D.CID = 3
D.id = 3, open=true, D.CID = 3
D.id = 4, open=true, D.CID = 4
C表
C.id = 2, A.id = 3, B.id = 5
C.id = 3, A.id = 3, B.id = 5
C.id = 4, A.id = 4, B.id = 6
Bc D中的前3个条目具有相同AID和BID的CID,它们在同一条目中计数。因此,查询应返回以下两个条目:
1. count = 3 A.id = 3 B.id = 5
2. count = 1 A.id = 4 B.id = 6
我为AB制作了一个元组表,因为我认为它的工作方式类似于以下的伪代码
For each entry i in tuple Table AB
for each entry j in Table C
for each entry k in Table D
if j.AID AND j.BID equal to i.AID and i.BID
And if k.CID = j.id
Then returnHash[i] = returnHash[i] + 1
- 可能没有必要,但
答案 0 :(得分:1)
SELECT
COUNT(*),
c.aid,
c.bid
FROM
d INNER JOIN c
ON
d.cid = c.id
WHERE
d.open
GROUP BY
c.aid,
c.bid;