0

我正在尝试使用此查询仅获取具有唯一区域 = 200 的 rtp.releaseid

select rtp.ReleaseId, rtp.TerritoryId  from ReleaseTerritoryPrice rtp where 
rtp.TerritoryId=200 

但我想有些东西不见了,你能帮忙吗?谢谢。

4

4 回答 4

3

NOT EXISTS您可以在 WHERE 子句中使用以下内容:

select rtp1.releaseId, rtp1.territoryId
from ReleaseTerritoryPrice rtp1
where rtp1.territoryId = 200
  and not exists (select releaseId
                  from ReleaseTerritoryPrice t2
                  where t2.territoryId <> 200
                    and rtp1.releaseId = t2.releaseId);

请参阅带有演示的 SQL Fiddle

或者您可以NOT IN在 WHERE 子句中使用:

select rtp1.releaseId, rtp1.territoryId
from ReleaseTerritoryPrice rtp1
where rtp1.territoryId = 200
  and rtp1.releaseId not in (select releaseId
                              from ReleaseTerritoryPrice t2
                              where t2.territoryId <> 200);

请参阅带有演示的 SQL Fiddle

于 2013-06-06T14:33:22.710 回答
0

也许你的意思是这个

SELECT MAIN.* FROM ReleaseTerritoryPrice MAIN
WHERE MAIN.ReleaseId in (
    SELECT rtp.ReleaseId
    FROM   ReleaseTerritoryPrice rtp
    WHERE rtp.TerritoryId =200
    GROUP BY rtp.ReleaseId
    HAVING COUNT(rtp.TerritoryId) =1 )
于 2013-06-06T14:37:34.637 回答
0

几种方法

SELECT ReleaseId
FROM   ReleaseTerritoryPrice
WHERE  TerritoryId = 200 
EXCEPT
SELECT ReleaseId
FROM   ReleaseTerritoryPrice
WHERE  TerritoryId IS NULL OR TerritoryId <> 200 

或者

SELECT ReleaseId
FROM   ReleaseTerritoryPrice
GROUP BY ReleaseId
HAVING COUNT(DISTINCT TerritoryId) =1 AND MAX(TerritoryId )=200
于 2013-06-06T14:31:28.470 回答
-1

使用DISTINCT关键字。

select DISTINCT rtp.ReleaseId, 
     rtp.TerritoryId  
from ReleaseTerritoryPrice rtp 
where rtp.TerritoryId=200
于 2013-06-06T14:29:11.093 回答