I want to make three type of queries:
SELECT * FROM table
SELECT * FROM table WHERE code IN (...)
SELECT * FROM table WHERE code IN (...) AND username = 'xxx'
SELECT * FROM table WHERE username = 'xxx'
I know I can build the query using 'if' sentences depending on the parameters, but I don't sure that it is well or not, for example I can use a preparedStatement like:
SELECT * FROM table WHERE code IN(?) AND username = ?
But there's another problem, because, how to avoid (code IN(?)) or (AND username) depending on the parameters? The only way I know is using string concatenation like:
if (codes is not null) then
query = query + " WHERE code IN( codes )"
if (username is not null) then
query = query + " AND username = ? "
There is possible to build a unique query in a preparedStatement using mysql?
LIKE:
SELECT * FROM table WHERE if (codes is not null) code IN ( ? ) AND if (username is not null) username = 'xxx'