我有两张桌子:表A
AId x ImageCount
1-1-1
2/2-1
3/2
表B
BId x AForeignKey
1-1-1
2/2-3
3-3-3
我得到了这个查询,这给了我一个可视化的比较值:
从A RecordsInB中选择t1.AId,t1.ImageCount,COUNT(t2.AForeignKey)作为RecordsInB,左转tw.AForeignKey = t1.AId组,t1.ImageCount
但我想不出如何消除那些ImageCount不等于RecordsInB的行。我真正关心的是AId列,但是我在上面的查询中显示其他列,这样我就可以在视觉上进行比较。
因此,输出应该如下所示:
AId
1
3.
或作视觉比较:
AId|ImageCount|RecordsInB
1\x{e76f}1
3\x{e76f}2
我希望这是有意义的。
发布于 2017-04-06 22:30:47
如果我理解得很好,您需要过滤查询的结果,以便只保留行having t1.imageCount = COUNT(t2.AForeignKey)。
如果是这样,只需将此条件添加到查询中:
SELECT t1.AId, t1.ImageCount, COUNT(t2.AForeignKey) AS RecordsInB
FROM tableA t1
LEFT JOIN tableB t2 ON t2.AForeignKey = t1.AId
GROUP BY t1.AId, t1.ImageCount
having t1.imageCount = COUNT(t2.AForeignKey)https://stackoverflow.com/questions/43266561
复制相似问题