Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use "WHERE" clause to select all records in SQL Statement?

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 ";
like image 218
薛源少 Avatar asked Oct 17 '13 11:10

薛源少


3 Answers

Try with WHERE 1=1::

Select * from myTable  WHERE 1=1
like image 192
Sashi Kant Avatar answered Sep 30 '22 16:09

Sashi Kant


This also works:

WHERE columnname LIKE '%'

Except for NULL values.

like image 41
Pradeep Simha Avatar answered Sep 30 '22 16:09

Pradeep Simha


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

like image 20
saswat panda Avatar answered Sep 30 '22 15:09

saswat panda