I have an Mysql query
SELECT *
FROM EMPLOYEE
WHERE DEPARTMENT = ?
AND DESIGNATION = ?
AND DISTRICT = ?
AND CIRCLE = ?
And quite possible any 1 or 2 or 3 of the parameter (?) can be empty or null.
so what should I do so that the empty parameters are totally
Ignorein the where clause andonly search for the non-empty parameterin the table.
How can I achieve this
Please help.. The query must be compatible mysql. Thanks
You could add the input parameters twice and make the query like this:
SELECT *
FROM EMPLOYEE
WHERE (DEPARTMENT = ? OR ? IS NULL)
AND (DESIGNATION = ? OR ? IS NULL)
AND (DISTRICT = ? OR ? IS NULL)
AND (CIRCLE = ? OR ? IS NULL)
So on the first and second '?' you bind the same value.
Alternatively (and arguably better), you can build the SQL dynamically, and leave out certain parts of the where clause if it doesn't apply. You can concat the strings to get the basic SQL and still use bind parameters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With