Thursday, 3 October 2013

mysql select where if variable is set

mysql select where if variable is set

I have two tables:
sales
sales_sizes
I want to do a query for sales_sizes constrained by the variables $gender,
$category and $brand.
How do I do a where statement if the individual variables are set, and
don't include them in the query if they are not set?
[pseudo code in brackets]
SELECT size, country
FROM `sales_sizes` WHERE sale_id IN (
SELECT sale_id
FROM `sales`
WHERE [gender = $gender if $gender isset]
AND [category = $category if $category isset]
AND [brand = $brand if $brand isset]
)