skip to Main Content

I have a example scenario, where I have a table with 3 columns which are

feature_id
value
product

I want to get all the products where feature_id is 1 or 3 and value is "Yes". Following query in sql give me empty result set

SELECT * FROM product
WHERE
(feature_id = 1 AND Value = "Yes")
AND
(feature_id = 3 AND Value = "Yes")

Table image is also attached:

enter image description here

2

Answers


  1. In your current syntax feature_id needs to be both 1 and 3 at the same time, which is impossible. You need to change AND to OR:

    SELECT * FROM product
    WHERE
    (feature_id = 1 AND value = "Yes")
    OR
    (feature_id = 3 AND value = "Yes")
    

    Or better:

    SELECT * FROM product
    WHERE feature_id IN (1, 3) 
      AND value = "Yes"
    
    Login or Signup to reply.
  2. you can make a combination of OR and AND

    SELECT * FROM product
    WHERE
    (feature_id = 1 OR feature_id = 3)   AND Value = "Yes")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search