I have a requirement to build a versatile parameterized SELECT query which fetches data from a mysql table and optionally filters that data by values from a column in one fell swoop.
I know this can be achieved with mutltiple SELECTs, but it has to be just one statement.
The query either fetches all data if a NULL is passed or fetches filtered data if a value is passed in other than NULL.
One of the reasons for this requirement is we are externalizing our SQL strings.
I have managed to come up with this:
SELECT * FROM `table` WHERE `column` = COALESCE(NULLIF(?,''),`column`);
I pass in a NULL when I need to fetch unfiltered data and it works smoothly, efficiently.
However, it never fetches data for which **column** is NULL.
How do I make it fetch columns with NULL data?
WHERE column IS NULL?