我有以下表格:
我想获得所有与文章“5”具有相同标签的文章,我试着这样:
SELECT DISTINCT Article.*
FROM Article
INNER JOIN ArticleTag ON Article.idArticle = ArticleTag.idArticle
AND ArticleTag.idArticle = "5"
WHERE 1 = 1
ORDER BY title
LIMIT 40
但是它显示了所有文章,而不是相关文章。
答案 0 :(得分:2)
尝试
select a.* from Article a
inner join ArticleTag at
on at.idArticle = a.idArticle
where at.idTag in (select idTag from ArticleTag where idArticle =5)
或
select a.* from Article a
inner join ArticleTag at on at.idArticle= a.idArticle
inner join ArticleTag at2 on at2.idTag = a.idTag and at2.IdArticle! = at.idArticle
where at2.idArticle = 5
答案 1 :(得分:1)
SELECT DISTINCT Article.*
FROM Article
INNER JOIN ArticleTag ON Article.idArticle = ArticleTag.idArticle
WHERE ArticleTag.idTag IN
(SELECT ArticleTag.idTag FROM ArticleTag WHERE ArticleTag.idArticle = '5')
ORDER BY Article.title
LIMIT 40;