Q:

How to use IN operator with AND operator in WHERE clause in SQL...?

I am using this database query in my CodeIgniter model.
SELECT
                p.product_id,
                p.product_name,
                p.product_photo,
                p.size,
                p.price,
                p.status,
                p.product_image_path
            FROM
                products AS p
            LEFT JOIN
                product_category AS pc
            ON
                p.product_id = pc.product_id
            LEFT JOIN
                vendor_products AS vp
            ON
                vp.product_id = pc.product_id
            WHERE
                pc.category_id = 2
            AND
                vp.vendor_id = 36
            AND 
                pc.subcategory_id IN (1,2)
            AND
                pc.subcategory_value_id IN (1,4)

And it returning me:

I want those products only who fills all conditions of sub_category_value_id. Now it is returning all the conditions.
I am new to database and don't know much about queries.

A:

Below query will give you distinct products with product_id and product_name on basis of given vendor_id, category_id, subcategory_id's and subcategory_value_id's.
Let me know if this works for you!
SELECT 
DISTINCT p.product_id,
p.product_name 
FROM products AS p
LEFT JOIN product_category AS pc ON p.product_id = pc.product_id
LEFT JOIN vendor_products AS vp ON p.product_id = vp.product_id
LEFT JOIN subcategories AS sc ON sc.subcategory_id = pc.subcategory_id
LEFT JOIN subcategories_value AS scv ON scv.subcategory_value_id = pc.subcategory_value_id
WHERE vp.vendor_id = 2 
    AND vp.category_id = 2 
    AND pc.subcategory_id IN (1, 2) 
    AND scv.subcategory_value_id IN (1, 4)
ORDER BY p.product_id;

