0

我有一个包含两列 content_id 和 geography_id 的表,有许多内容 id 被标记到多个地理位置。如何仅获取标记到特定地理位置而不是任何其他地理位置的内容 ID。

TIA, Ankit

4

4 回答 4

2

像这样的东西可能是您正在寻找的东西:

select content_id
from the_table
group by content_id
having count(distinct geography_id) = 1
   and max(geography_id) = 126 -- this is the geography_id you are looking for

对两个 ID 执行此操作会稍微改变语句:

select content_id
from the_table t1
where geography_id in (11,12)
group by content_id
having count(distinct geography_id) = 2

如果您需要使用这两个 geography_id 来获取那些 region_id,那就有点复杂了:

select t1.content_id
from the_table t1
where t1.geography_id in (11,12)
group by t1.content_id
having count(distinct t1.geography_id) = 2
   and count(distinct t1.geography_id) = (select count(*) 
                                          from the_table t2
                                          where t2.content_id = t1.content_id)

或者作为替代方案:

select content_id
from the_table
where geography_id = 11
intersect
select content_id
from the_table
where geography_id = 12
minus  -- this would be intersect for any other DBMS
select content_id
from the_table
group by content_id
having count(distinct geography_id) > 2;

SQLFiddle 示例:http ://sqlfiddle.com/#!4/35823/7

于 2013-05-16T15:02:30.913 回答
1

我认为这很简单。您只需要对记录进行分组content_ID并计算geography_id应该等于一)的数量。

SELECT  content_ID
FROM    TableName
GROUP   BY content_ID
HAVING  COUNT(DISTINCT geography_id) = 1 AND
        MAX(geography_id) = 'specific tag'
于 2013-05-16T15:02:03.783 回答
0

@a_horse_with_no_name 你是对的,我的回答是错误的。

正确答案是

SELECT content_id
from TableName
where content_id in (
SELECT  content_ID
FROM    TableName
WHERE   geography_id in (11)
)
group by content_id
having count(distinct geography_id)=1
于 2013-05-16T15:04:41.863 回答
0

如果你的意思只是不重复的 contentId 那么

select contentid
from mytable
group by contentid
having count(*)=1

这将返回仅映射到一个 geoghraphyId 的那些 contentId。

SQL 小提琴

于 2013-05-16T15:07:21.953 回答