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.
WHERE Price < '$300'do you really store prices as a$char prefixed strings?IF (Item = 'Dresses', 1, 0) = 1- this makes no sense. So trying to solve it "in the similar way" is wrong by design.IFand 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)