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.
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
)
You can do what you want with a group by
and having
clause:
select article
from categories_article
where categorie in (1, 11, 13, 22)
group by article
having count(*) = 4;
If you can have duplicates in the table, use count(distinct categorie) = 4
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.