1

I have a database with multiple columns. Now i have a Java program where i can search for different rows in my database. But i need help for the SQL SELECT request. I have some texfields and some checkboxes. Now i want if the textfields are empty, or "any" is selected in one of the checkboxes , the select request should select ALL from this column.

Here is my request right now:

resultSet = sqlStatement.executeQuery("SELECT * FROM Messgerate WHERE COLUMN1 LIKE '%"+sColumn1+"%'" +
"AND COLUMN2 LIKE '%"+sColumn2+"%'" +
"AND COLUMN3 LIKE '%"+sColumn3+"%'" +
"AND COLUMN4 LIKE '%"+sColumn4+"%'" +
"AND COLUMN5 LIKE '%"+sColumn5+"%'" +
"AND COLUMN6 LIKE '%"+sColumn6+"%'" +"");

This request ist working if i type in something in all of the textfields and checkboxes.

Can someone help me please?

1

3 Answers 3

1

Can you replace AND with OR. It ll solve your issue. Because the AND operator displays a record if both the first condition AND the second condition are true.

The OR operator displays a record if either the first condition OR the second condition is true.

"SELECT * FROM Messgerate WHERE COLUMN1 LIKE '%"+sColumn1+"%'" +
"OR COLUMN2 LIKE '%"+sColumn2+"%'"
"OR COLUMN3 LIKE '%"+sColumn3+"%'" +
"OR COLUMN4 LIKE '%"+sColumn4+"%'" +
"OR COLUMN5 LIKE '%"+sColumn5+"%'" +
"OR COLUMN6 LIKE '%"+sColumn6+"%'" +"");
Sign up to request clarification or add additional context in comments.

6 Comments

but if i use OR , just one of all statements must me true , right? but i want all statements must be true
Yes, if you want all statements must be true, use AND and check Which checkboxes are selected and pass those values
If AND, all the column should be matched
is there no easier way? Because the code will be huge if i have to write so many requests
You want if the textfields are empty, or "any" is selected in one of the checkboxes. Just check it
|
0

You should pass an empty string when Any is selected in the dropdown. And then you can use this:

resultSet = sqlStatement.executeQuery("SELECT * FROM Messgerate WHERE COLUMN1 LIKE '%"+sColumn1+"%'" +
"OR COLUMN2 LIKE '%"+sColumn2+"%'" +
"OR COLUMN3 LIKE '%"+sColumn3+"%'" +
"OR COLUMN4 LIKE '%"+sColumn4+"%'" +
"OR COLUMN5 LIKE '%"+sColumn5+"%'" +
"OR COLUMN6 LIKE '%"+sColumn6+"%'" +"");

Comments

0
select * from messgerate where case when isnull(sColumn1,'') = '' then '' when isnull(sColumn1,'') = 'any' then '' else COLUMN1 end LIKE '%"+sColumn1+"%'"

Repeat the case statement for the rest of the columns

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.