My table categories_article looks like this :
I want to return all articles which have categorie 1 and 11 and 13 and 22. I am trying to do it like this :
select * from categories_article where categorie in(1,11,13,22)
But the request return article 13, and in this example the article 13 doesn't have categorie 22.
Please somebody help me thank you.
答案 0 :(得分:1)
You will want to use an EXISTS
statement to make sure that each of those categories are found for the articles. There's bound to be a better way to do this, but here's a solution:
Select *
From categories_article A
Where Exists
(
Select *
From categories_article B
Where A.article = B.article
And B.categorie = 1
)
And Exists
(
Select *
From categories_article B
Where A.article = B.article
And B.categorie = 11
)
And Exists
(
Select *
From categories_article B
Where A.article = B.article
And B.categorie = 13
)
And Exists
(
Select *
From categories_article B
Where A.article = B.article
And B.categorie = 22
)
答案 1 :(得分:1)
您可以使用group by
和having
子句执行您想要的操作:
select article
from categories_article
where categorie in (1, 11, 13, 22)
group by article
having count(*) = 4;
如果表格中有重复项,请使用count(distinct categorie) = 4
。