5

Good Evening, I am curious if it is possible to make a WHERE-clause in a SQL statement which can show all records?

Below some explanation:

Random SQL Statement (Java)-(JSP example), Normal Situation

String SqlStatement = "SELECT * FROM table_example WHERE First_Col = '<%=passVar%>' ";
db.query(SqlStatement );
//........
//........

What if the passVar is 'ALL', and we need to prompt all the records out when passVar = All? I know I can do it with if-else and check if the passVar is "ALL" then query the without-WHERE statement to make it work..

**without-WHERE statement (Java)-(JSP example)**
if(<%=passVar%> == "ALL") {
   SqlStatement = "SELECT * FROM table_example";
} else {

   SqlStatement = "SELECT * FROM table_example WHERE First_Col = '<%=passVar%>' ";
}

but can I just code one SQL statement to make all the records prompt? Something like below:

(Java)-(JSP example)

String ShowAll = "";
if(<%=passVar%> == "ALL") {
    ShowAll = *;

} else {
    ShowAll = <%=passVar%>;
}
SqlStatement = "SELECT * FROM table_example WHERE First_Col = ShowAll ";
lt.kraken
  • 1,287
  • 3
  • 10
  • 27
薛源少
  • 306
  • 1
  • 6
  • 18

8 Answers8

14

Try with WHERE 1=1::

Select * from myTable  WHERE 1=1
Sashi Kant
  • 13,277
  • 9
  • 44
  • 71
4

This also works:

WHERE columnname LIKE '%'

Except for NULL values.

Pradeep Simha
  • 17,683
  • 18
  • 56
  • 107
3

where 1=1 worked for me, Although where clause was being used all records were selected.

You can also try

SELECT * FROM Customers
WHERE CustomerID=CustomerID; /* query */

or [any_column_name]=[column_name_in_LHL]

(LHL=left hand side.)

copy the query and click here to try code

DennisLi
  • 3,915
  • 6
  • 30
  • 66
saswat panda
  • 151
  • 1
  • 5
2

It would be better to differ the 2 situations and make 2 queries out of it.

  • If there is no where condition then the DB does not need to evaluate it (potencially faster)
  • The source code/debugging output is clearer.
juergen d
  • 201,996
  • 37
  • 293
  • 362
  • ya, currently I using two query... but one of my friends has mentioned that, when the program getting larger/ bigger then it is better to find a ways to make one query instead of 2 or more, it's optional... thanks... – 薛源少 Oct 18 '13 at 01:49
  • You know - this made so much sense to me I completely stopped with this line of thinking and just made one more query. The difference between a few extra lines of code and the nightmares of debugging a conditional query is a no-brainer. – Iofacture Jun 18 '19 at 03:06
1

Consider moving the special case inside the query itself, e.g.

SELECT * FROM table_example WHERE '<%=passVar%>' IN ('ALL', First_Col)
Dan
  • 4,312
  • 16
  • 28
0

Try with wildcard value '%' but I would recommend to use a Factory here to create the SQL statement, what you are trying to do smells a bit.

RamonBoza
  • 8,898
  • 6
  • 36
  • 48
0

Something else you could do, is making that combination of code and SQL a single query. Which means the IF..ELSE will be in SQL language.

Check these links for some more info:
MySQL
Using If else in SQL Select statement

Community
  • 1
  • 1
lt.kraken
  • 1,287
  • 3
  • 10
  • 27
-1

On sqlserver you can make proc:

create proc select_all_on_null
@a int = null
as
begin
  select * from Records where (@a is null or Record_id=@a )
end

When you select be your program:

make @a in null will select all 

if i is number there will select row with this id

Jeevan ebi
  • 105
  • 12