I've got a little sql question.
Let's say you have 4 drop-down boxes, and the user can select different options in all 4 boxes, then hit submit. The values get passed to a
servlet which does a query to a database with the restrictions specified in the box.
ex:
Select system_names
from tb__systems
where option1=box1
and option2=box2
and option3=box3
and option4=box4;
(of course option and box are replaced by real values, I just can't be bothered writing the whole thing).
I have managed to get this to work fine, but what if you don't want to narrow it down that much. What if you only want to select 2 options, and leave the other 2 at "any".
How do you write a piece of code (I was thinking it would have to be an "if" statement) that checks for this, and makes the query
string accordingly?
Here's what I had with just 2 boxes:
if(env != "any")
{
query = querybase + "env_identifier=" + env;
if(loc != "any")
query = query + "and loc_identifier=" + loc;
else
query = query;
}else
{
if(loc != "any")
query = querybase + "loc_identifier = " + loc;
else
query = "select system_name from tb__systems";
}
(I hope the code came out ok)
Any ideas? what am I doing wrong?
Thanks,
Annette