Java 是否可以使用“WHERE”子句来选择 SQL 语句中的所有记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19425298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to use "WHERE" clause to select all records in SQL Statement?
提问by u1505885
Good Evening, I am curious if it is possible to make a WHERE-clause in a SQL statement which can show all records?
晚上好,我很好奇是否可以在可以显示所有记录的 SQL 语句中创建一个 WHERE 子句?
Below some explanation:
下面做一些解释:
Random SQL Statement (Java)-(JSP example), Normal Situation
随机SQL语句(Java)-(JSP示例),正常情况
String SqlStatement = "SELECT * FROM table_example WHERE First_Col = '<%=passVar%>' ";
db.query(SqlStatement );
//........
//........
What if the passVaris '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 passVaris "ALL" then query the without-WHERE statementto make it work..
如果passVar是'ALL',我们需要在passVar= All的时候把所有的记录都提示出来怎么办?我知道我可以使用 if-else 并检查passVar是否为“ALL”,然后查询without-WHERE 语句以使其工作。
**without-WHERE statement (Java)-(JSP example)**
**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:
但是我可以只编写一个 SQL 语句来使所有记录都提示吗?像下面这样:
(Java)-(JSP example)
( Java)-( JSP 示例)
String ShowAll = "";
if(<%=passVar%> == "ALL") {
ShowAll = *;
} else {
ShowAll = <%=passVar%>;
}
SqlStatement = "SELECT * FROM table_example WHERE First_Col = ShowAll ";
采纳答案by Pradeep Simha
This also works:
这也有效:
WHERE columnname LIKE '%'
Except for NULL values.
除了 NULL 值。
回答by Sashi Kant
Try with WHERE 1=1
::
试试 WHERE 1=1
::
Select * from myTable WHERE 1=1
回答by RamonBoza
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.
尝试使用通配符值,'%'
但我建议在此处使用工厂来创建 SQL 语句,您尝试执行的操作有点味道。
回答by Dan
Consider moving the special case inside the query itself, e.g.
考虑在查询本身内部移动特殊情况,例如
SELECT * FROM table_example WHERE '<%=passVar%>' IN ('ALL', First_Col)
回答by larssy1
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.
您还可以做的其他事情是使代码和 SQL 的组合成为单个查询。这意味着 IF..ELSE 将使用 SQL 语言。
Check these links for some more info:
MySQL
Using If else in SQL Select statement
检查这些链接以获取更多信息:
MySQL
Using If else in SQL Select statement
回答by juergen d
It would be better to differ the 2 situations and make 2 queries out of it.
最好区分这两种情况并从中进行 2 次查询。
- If there is no where condition then the DB does not need to evaluate it (potencially faster)
- The source code/debugging output is clearer.
- 如果没有 where 条件,那么数据库不需要评估它(可能更快)
- 源代码/调试输出更清晰。
回答by Bahaa Qurini
On sqlserver you can make proc:
在 sqlserver 上,您可以进行 proc:
create proc select_all_on_null
@a int
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 numder
there will select row with this id
如果我在numder
那里将选择具有此 ID 的行
回答by saswat panda
where 1=1 worked for me, Although where clause was being used all records were selected.
where 1=1 对我有用,尽管使用了 where 子句,但所有记录都被选中。
You can also try
你也可以试试
SELECT * FROM Customers
WHERE CustomerID=CustomerID; /* query */
or [any_column_name]=[column_name_in_LHL]
或 [any_column_name]=[column_name_in_LHL]
(LHL=left hand side.)
(LHL=左侧。)
copy the query and click here to try code
复制查询并 单击此处尝试代码