Mysql 检查两件物品之间是否有共同标签

Mysql 检查两件物品之间是否有共同标签,mysql,sql,select,group-by,compare,Mysql,Sql,Select,Group By,Compare,我有两个项目ID(1和2)。我需要看看他们之间是否有共同的标签。某种交叉 在这种情况下,结果将是:标记1在这两种情况下都是通用的。一篇文章可能有几个标签 大概是这样的: | id | tag | article |----+-----+--------- | | 1 | 1 | | 1 | 2 | | 3 | 2 | | 5 | 2 |----+-----+-------- 下面的sql将给出结果 select tag from table wher

我有两个项目ID(1和2)。我需要看看他们之间是否有共同的标签。某种交叉

在这种情况下,结果将是:标记1在这两种情况下都是通用的。一篇文章可能有几个标签

大概是这样的:

| id | tag | article 
|----+-----+---------
|    |  1  | 1
|    |  1  | 2
|    |  3  | 2
|    |  5  | 2
|----+-----+--------

下面的sql将给出结果

select tag from table
 where ( select tag from table where article = 1) =
       ( select tag from table where article = 2)
试试这个:

SELECT 1 FROM your_table a
INNER JOIN your_table b ON a.tag = b.tag
WHERE a.article = 1 AND b.article = 2
联接和计数(并阅读SQL教程)。
SELECT tag 
FROM table WHERE article IN (1, 2) 
GROUP BY tag HAVING COUNT(DISTINCT article) = 2