我有一个表格如下:
User ID Service
1 2
1 3
2 1
2 3
3 5
3 3
4 3
5 2
如何构造一个查询来计算拥有 3 项服务和至少一项其他服务的所有用户 ID?
在上表中,我感兴趣的查询将返回 3,因为用户 ID 1、2 和 3 拥有服务 3 以及至少一项其他服务。
谢谢!
最佳答案
在 MS SQL 中:
select count(*)
from UserService
where
ServiceId = 3 and
UserId in (select UserId from UserService where ServiceId != 3)
关于SQL 计算行上给定复合条件的值的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6159153/