简体   繁体   中英

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.

It seems like you shouldn't use left join to product_category table. The LEFT JOIN keyword returns all records from the left table ( product_category ) Please try like below:

SELECT
                p.product_id,
                p.product_name,
                p.product_photo,
                p.size,
                p.price,
                p.status,
                p.product_image_path
            FROM
                products AS p
            INNER 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)

You want group by and having . It is a little unclear what you mean by "all conditions", but it would look something like this:

SELECT p.*
FROM products p JOIN
     product_category pc
     ON p.product_id = pc.product_id 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, pc.subcategory_value_id) IN ( (1, 1), (2, 4) )
GROUP BY p.product_id  -- this is okay because it is presumably the primary key
HAVING COUNT(DISTINCT pc.subcategory_id) = 2;

Notes:

  • The WHERE clause turns the LEFT JOIN into an INNER JOIN , so use the right JOIN .
  • I assume you want pairs of values from the subcategory table.
  • The HAVING clause insists on both subcatgories matching.

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;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM