在MySQL中减少?

时间:2008-10-08 20:02:41

标签: sql mysql

我有主题(id *)和标签(id *,名称)和链接表topic_tags(topicFk *,tagFk *)。

现在我想选择每个主题,包含所有好标签(a,b,c),但没有坏标签(d,e,f)。

我该怎么做?

7 个答案:

答案 0 :(得分:5)

假设您的Topic_Tags表格是唯一的,这会回答您的完全问题 - 但可能无法解决您的实际问题:

SELECT
  TopicId
FROM Topic_Tags
JOIN Tags ON
  Topic_Tags.TagId = Tags.TagId
WHERE
  Tags.Name IN ('A', 'B', 'C', 'D', 'E', 'F')
GROUP BY
  TopicId
HAVING
  COUNT(*) = 3 
  AND MAX(Tags.Name) = 'C'

更通用的解决方案是:

SELECT 
    * 
FROM (
    SELECT
        TopicId
    FROM Topic_Tags
    JOIN Tags ON
        Topic_Tags.TagId = Tags.TagId
    WHERE
        Tags.Name IN ('A', 'B', 'C')
    GROUP BY
        TopicId
    HAVING
        COUNT(*) = 3 
) as GoodTags
LEFT JOIN (
    SELECT
        TopicId
    FROM Topic_Tags
    JOIN Tags ON
        Topic_Tags.TagId = Tags.TagId
    WHERE
        Tags.Name = 'D'
        OR Tags.Name = 'E'
        OR Tags.Name = 'F'
) as BadTags ON
    GoodTags.TopicId = BadTags.TopicId
WHERE
    BadTags.TopicId IS NULL

答案 1 :(得分:3)

这是一个可行的解决方案,但需要为您需要的每个标记提供连接。

SELECT *
FROM topics
WHERE topic_id IN
    (SELECT topic_id
    FROM topic_tags a
    INNER JOIN topic_tags b
      on a.topic_id=b.topic_id
      and b.tag = 'b'
    INNER JOIN topic_tags c
      on b.topic_id=c.topic_d
      and c.tag = 'c'
    WHERE a.tag = 'a')
AND topic_id NOT IN
    (SELECT topic_id
    FROM topic_tags
    WHERE tag = 'd' or tag = 'e' or tag = 'f')

答案 2 :(得分:3)

这是另一个替代查询。也许将好的和坏的标签列表放在顶部更清晰,更方便。我在MySQL 5.0上测试了这个。

SELECT t.*, 
  SUM(CASE WHEN g.name IN ('a', 'b', 'c') THEN 1 ELSE 0 END) AS num_good_tags,
  SUM(CASE WHEN g.name IN ('d', 'e', 'f') THEN 1 ELSE 0 END) AS num_bad_tags
FROM topics AS t
 JOIN topic_tags AS tg ON (t.id = tg.topicFk)
 JOIN tags AS g ON (g.id = tg.tagFk)
GROUP BY t.id
HAVING num_good_tags = 3 AND num_bad_tags = 0;

答案 3 :(得分:1)

正如所写,其他3个答案进来了,但这是不同的所以我会发布它。

我们的想法是选择包含a,b,c标签的所有主题,然后使用左连接标识那些也包含d,e,f的主题,然后使用where子句过滤那些主题,并在其上查找空值加入...

select distinct topics.id from topics 
inner join topic_tags as t1 
    on (t1.topicFK=topics.id)
inner join tags as goodtags 
    on(goodtags.id=t1.tagFK and goodtags.name in ('a', 'b', 'c'))
left join topic_tags as t2 
    on (t2.topicFK=topics.id)
left join tags as badtags 
    on(badtags .id=t2.tagFK and batags.name in ('d', 'e', 'f'))
where badtags.name is null;

完全没有经过测试,但希望你能看到逻辑的来源。

答案 4 :(得分:0)

我不完全确定,我希望有更好的方法来做好标签部分,但是:

select id from topic
    inner join topic_tags tta on topic.id=tta.topicFk and tta.tagFk=a
    inner join topic_tags ttb on topic.id=ttb.topicFk and ttb.tagFk=b
    inner join topic_tags ttc on topic.id=ttc.topicFk and ttc.tagFk=c
    left join topic_tags tt on topic.id=tt.topicFk and tt.tagFk in (d,e,f)
    where tt.topicFk is null;

更新:这样的事情:

select id from topic
    left join topic_tags tt on topic.id=tt.topicFk and tt.tagFk in (d,e,f)
    where tt.topicFk is null and
        3=(select count(*) from topic_tags where topicFk=topic.id and tagFk in (a,b,c));

我看到一个答案,假设a,b,c,d,e,f是名称,而不是ID。如果是这样,那么:

select id from topic
    left join topic_tags tt on topic.id=tt.topicFk
        inner join tags on tt.tagFk=tags.id and tags.name in (d,e,f)
    where tt.topicFk is null and
       3=(select count(*) from tags inner join topic_tags on tags.id=topic_tags.tagFk and topic_tags.topicFk=topic.id where tags.name in (a,b,c));

答案 5 :(得分:0)

您可以使用minus关键字来过滤掉有不良标记的主题。

-- All topics with desired tags.
select distinct T.*
from Topics T inner join Topics_Tags R on T.id = R.topicFK
              inner join Tags U on U.id = R.topic=FK
where U.name in ('a', 'b', 'c')

minus

-- All topics with undesired tags. These are filtered out.
select distinct T.*
from Topics T inner join Topics_Tags R on T.id = R.topicFK
              inner join Tags U on U.id = R.topic=FK
where U.name in ('d', 'e', 'f')

答案 6 :(得分:0)

我自己的解决方案使用Pauls and Bills的想法。

我们的想法是使用好的标签来内连接主题(抛弃没有好标签的主题),然后计算每个主题的唯一标签(以验证所有好的标签都存在)。

同时,带有错误标记的外部联接应该没有一个匹配项(所有字段都是NULL)。

SELECT topics.id
FROM topics
  INNER JOIN topic_tags topic_ptags
    ON topics.id = topic_ptags.topicFk
  INNER JOIN tags ptags
    ON topic_ptags.tagFk = ptags.id
      AND ptags.name IN ('a','b','c')
  LEFT JOIN topic_tags topic_ntags
    ON topics.id = topic_ntags.topicFk
  LEFT JOIN tags ntags
    ON topic_ntags.tagFk = ntags.id
      AND ntags.name IN ('d','e','f')
GROUP BY topics.id
HAVING count(DISTINCT ptags.id) = 3
  AND count(ntags.id) = 0