我是这个论坛的新手,也是 SQL 新手,需要在工作中快速查看。我搜索了这样的主题,但没有找到完全相同的问题...
我有 2 个表:TableA 和 TableB
表A
Name NumberOfScreens
-----------------------
ABC1 5
ABC2 4
ABC3 7
表B
Name Date Duration
------------------------
ABC1 x xxx
ABC1 x xxx
ABC1 x xxx
ABC3 x xxx
ABC3 x xxx
我现在的问题是计算 TableB 中相同名称的数量(这就是我的想法)
Select(*) from TableB where Count(Name) <> TableA.NumberOfScreens
因此,如果 ABC1 在 TableB 中出现 5 次,并且 TableA.NumberOfScreens of ABC1 = 5,我不希望它出现在我的 SELECT 中。 如果 ABC3 在 TableB 中出现 3 次,并且 TableA.ABC3 的 NumberOfScreens = 5,我需要它出现在我的 SELECT 中。
有人可以帮助我吗?
谢谢:)
最佳答案
select A.Name, A.NumberOfScreens
from TableA A join TableB B
on A.Name = B.Name
group by A.Name,A.NumberOfScreens
having count(*) = A.NumberOfScreens
编辑:
select tb.Name, tb.Date, tb.Duration from TableB tb join ( select A.Name tmpName, A.NumberOfScreens tmpNumberOfScreen from TableA A join TableB B on A.Name = B.Name group by A.Name,A.NumberOfScreens having count(*) <> A.NumberOfScreens ) tmp on tb.name = tmp.tmpName
关于mysql - 使用 SQL 选择与其他表值匹配的计数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33798528/