I have three HTML Select tags. The contents are as below:
- All, Users, Guests
- All, Approved, Rejected
- All, Public, Private
I am doing following:
String by = //Some Code to get this value
String status = //Some Code to get this value
String privacy = //Some Code to get this value
String searchQuery = "SELECT * FROM requests";
if(!by.equalsIgnoreCase("all")){
searchQuery += " WHERE SEARCH_BY='" + by + "'";;
}
if(!status.equalsIgnoreCase("all")){
searchQuery += " WHERE SEARCH_STATUS='" + status + "'";;
}
if(!privacy.equalsIgnoreCase("all")){
searchQuery += " WHERE SEARCH_PRIVACY='" + privacy + "'";;
}
This will be a working query only if the user changes only one tag. But what if he changes two select tags: searchQuery will look like this:
SELECT * FROM requests WHERE SEARCH_BY='Users' WHERE SEARCH_STATUS='Rejected'
I know i can use AND keyword like : WHERE thisCondition AND thatCondition
But What is the possibility of knowing that thisCondition exists(only if this select tag is not changed), it will make my query like this: WHERE AND thatCondition
In the End, I need something like:
Select * FROM requests WHERE SEARCH_BY='A' AND SEARCH_STATUS='B' AND SEARCH_PRIVACY='C';
Where A, B & C can be either All or a value. When it is All, I want it to contain all the results from database.
Something like: WHERE SEARCH_BY='all' should return all the rows of database rather than matching values of SEARCH_BY column to all keyword.