Buddha Buddha - 2 months ago 6
MySQL Question

select multiple with and in one column

enter image description here

I need

article_id
where
cat_id=8
and
cat_id=7
and the result must return 70.

I tried this code but neither worked:

SELECT *
FROM andishkadeh_article_catgory
WHERE cat_id=6 AND cat_id=8

SELECT *
FROM andishkadeh_article_catgory
WHERE cat_id in (6,8)

Answer

Ok, I think I understood the question. If you need to find the article_id that has 2 different rows with values for cat_id of 7 and 8, then you can do:

SELECT DISTINCT article_id 
FROM andishkadeh_article_catgory a
WHERE cat_id = 7
AND EXISTS(SELECT 1 FROM andishkadeh_article_catgory
           WHERE cat_id = 8
           AND article_id = a.article_id)
Comments