1

I'm having trouble getting the syntax right for a MySQL SELECT statement with a subquery within the IF statement.


EDIT

I need to include subqueries in the db query if particular values have been specified. In this particular example, if they've specified the Item then only fetch rows where Item and Price also match specific values.

SELECT * FROM Products WHERE Products.Color = :Colors_like
AND IF(Item = IS NOT NULL OR Item != '' 
(Item = :Item AND Products.Price <= (SELECT $MaxPrice_Item FROM Lifestyle WHERE User_id = $User_id) ) 
) 

I've included a simplified snippet below with hardcoded values


This works correctly:

SELECT * FROM Products WHERE Color = 'Black'
AND IF (Item = 'Dresses', 1, 0) = 1

This doesn't work (If Item = Dresses, it should check Price)

SELECT * FROM Products WHERE Color = 'Black'
AND IF (Item = 'Dresses' (SELECT * FROM Products WHERE Price < '$300'), 1, 0) = 1

I've tried every format I could think of, but I can't seem to get the statement to work.

5
  • "This doesn't work" --- what should it do actually? The query is completely broken and it's hard to guess what you wanted to express with it. PS: WHERE Price < '$300' do you really store prices as a $ char prefixed strings? Commented Nov 23, 2014 at 23:55
  • @zerkms - I just clarified what it should do :-) (Apologies if I wasn't clear) Commented Nov 23, 2014 at 23:58
  • It still is not clear. Try to explain with domain language. Like: I need to select products that are black and ... and ... PS: you still didn't answer about prices PPS: IF (Item = 'Dresses', 1, 0) = 1 - this makes no sense. So trying to solve it "in the similar way" is wrong by design. Commented Nov 23, 2014 at 23:59
  • @zerkms - I'm only using the syntax of "...Item = 'Dresses', 1, 0) = 1" because I understood that's how to include an IF statement within a Select statement ;-) What I need to do is a Select statement WHERE ... (i.e. Color) AND IF ... (i.e. Item) THEN ... (i.e. Price). Does that help clarify it? Commented Nov 24, 2014 at 0:06
  • "because I understood that's how to include an IF " --- that's my exact point. You don't need IF and you never needed it. Anyway, nothing to discuss here anymore as Gordon has provided a good answer. (which still implies you need to clarify about how you store prices) Commented Nov 24, 2014 at 0:07

1 Answer 1

1

You need a something before the subselect:

SELECT *
FROM Products
WHERE Color = 'Black' AND
     IF (Item = 'Dresses' ?? (SELECT * FROM Products WHERE Price < '$300'), 1, 0) = 1
--------------------------^

I am not quite sure what, because there are many other problems, such as:

  • Using * where a scalar subquery is expected.
  • Using '$300' to presumably compare against a number. (Or worse, storing a "price" as a string.)
  • Having a scalar subquery return multiple rows.

If you want conditional logic, then this is what I think you want:

SELECT p.*
FROM Products p
WHERE p.Color = 'Black' AND
      (p.Item <> 'Dresses' OR p.Price < 300)
Sign up to request clarification or add additional context in comments.

2 Comments

@zerkms . . . I rephrased that part.
I'm not clear what I would need to add before the subset in order to get the IF statement to work, so I'm hoping that the information I just added to the question should help clarify what it is that I'm trying to do

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.