我有一个 MySQL 表,我正在尝试查询articles_data,但我不想与其他表进行任何联接。
它有行:
article_id doc_type doc_type_id
1 1 2
1 2 1
2 1 2
3 1 2
3 2 1
我正在尝试编写一个查询,该查询将返回 where doc_type
是 1 且 doc_type_id
是 2 并且它还有另一行相同的 article_id
哪里doc_type
是 2 且 doc_type_id
是 1 所以在上面的例子中我试图返回 article_id
1 和 3。
有什么想法可以编写 SQL 查询来执行此操作吗?
最佳答案
您不必联接到其他表,但可以通过自联接来实现
这是我的想法,您可能需要调整语法
select distinct article_id
from articles_data a
join articles_data b on b.articles_id = a.articles_id
where a.doc_type = 1 and a.doc_type_id = 2
and b.doc_type = 2 and b.doc_type_id = 1
关于没有 JOIN 的 MySQL 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6565615/