假设我有一个数据库表user_provider
,其中包含user
和provider
表的多对多关系。
我想只获得那些只有一个提供者的userId
SELECT spr1.SU_URS_ID,count(spr1.SU_URS_ID)
FROM overstappen.spr_usr spr1
WHERE spr1.SU_URS_ID in ( select spr.SU_URS_ID
from overstappen.spr_usr spr
where spr.SU_SPR_ID =40)
GROUP BY spr1.SU_URS_ID;
以上查询返回所有拥有SU_SPR_ID 40
的用户。并且spr_usr表中具有相同userId的用户总数为。
我只希望获得count 1
的用户。即只有一个提供者的用户。
提前致谢。
答案 0 :(得分:1)
试试这个
SELECT spr1.SU_URS_ID, COUNT(spr1.SU_URS_ID)
FROM overstappen.spr_usr spr1
WHERE spr1.SU_URS_ID IN
(SELECT spr.SU_URS_ID
FROM overstappen.spr_usr spr WHERE spr.SU_SPR_ID =40)
GROUP BY spr1.SU_URS_ID
HAVING COUNT(spr1.SU_URS_ID) = 1
答案 1 :(得分:1)
尝试在查询结尾处添加HAVING COUNT(spr1.SU_URS_ID)= 1:
SELECT * FROM users WHERE username='[username] and password=' ..