| id | tag | article
|----+-----+---------
| | 1 | 1
| | 1 | 2
| | 3 | 2
| | 5 | 2
|----+-----+--------
我有两篇文章ID(1和2)。我需要看看他们俩之间是否有共同的标签。某种交叉。
在这种情况下,结果将是:标签1在两者中都很常见。一篇文章可能有几个标签。
类似的东西:
select tag from table
where ( select tag from table where article = 1) =
( select tag from table where article = 2)
答案 0 :(得分:3)
下面的sql会给你结果。
SELECT 1 FROM your_table a
INNER JOIN your_table b ON a.tag = b.tag
WHERE a.article = 1 AND b.article = 2
答案 1 :(得分:0)
试试这个:
SELECT tag
FROM table WHERE article IN (1, 2)
GROUP BY tag HAVING COUNT(DISTINCT article) = 2