![](/img/trans.png)
[英]Oracle - SQL Query to return a value from a column in another table where value equals a different column
[英]Oracle SQL Query to retrieve records where a value in a column equals the count of values in another table
我有两个表:表A
AId | ImageCount
1 | 1个
2 | 1个
3 | 2
表B
投标 AF键
1 | 1个
2 | 3
3 | 3
我能够获得此查询,这使我可以直观地比较这些值:
选择t1.AId,t1.ImageCount,COUNT(t2.AForeignKey)AS从A1左联接B中的RecordsInB到t2。
但我不知道如何消除ImageCount不等于RecordsInB的那些行。 我真正关心的是AId列,但我在上面的查询中显示了其他列,以便可以直观地进行比较。
因此输出应如下所示:
援助
1个
3
或进行视觉比较:
AId | ImageCount | RecordsInB
1 | 1 | 1个
3 | 2 | 2
我希望这是有道理的。
如果我理解得很好,则需要过滤查询结果以仅保留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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.