Dynamic MySQL Where Clause in Stored Procedure
You can use CASE
statement to check for the value of @keywords
, eg.
SELECT col1, col2
FROM tblThreads
WHERE description LIKE CASE WHEN @keywords IS NULL
THEN description
ELSE CONCAT('%', @keywords, '%')
END
AND
price LIKE CASE WHEN @price IS NULL
THEN price
ELSE CONCAT('%', @price, '%')
END