MySQL SQL WHERE 列 = 一切
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10825247/
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
SQL WHERE column = everything
提问by Majo0od
Is SQL able to do something along the lines of this: SELECT * FROM table WHERE column = everything
SQL 是否能够执行以下操作: SELECT * FROM table WHERE column = everything
采纳答案by Eugen Rieck
Your question allow three intrepretations:
你的问题允许三种解释:
- You don't care about the column: drop the it from the where clause (or drop the where clause alltogether, if this was the only subclause)
- You want the column to be set, bit you don't care to what: Use
WHERE column IS NOT nULL
- You want a search, that can also display all records from a simple SQL template:
SELECT * FROM table WHERE column LIKE '%$searchterm%'
- 你不关心列:从 where 子句中删除它(或者完全删除 where 子句,如果这是唯一的子句)
- 您希望设置列,您不关心什么:使用
WHERE column IS NOT nULL
- 你想要一个搜索,它也可以显示一个简单的 SQL 模板中的所有记录:
SELECT * FROM table WHERE column LIKE '%$searchterm%'
回答by Robert Harvey
SELECT * FROM table
If you're generating SQL dynamically, it's
如果你动态生成 SQL,它是
SELECT * FROM table WHERE 1=1
The 1=1
placeholder allows you to return all records, or substitute an actual condition if you're returning a subset or need additional conditional statements.
该1=1
占位符可以让你回到所有记录,或者如果你返回一个子集或需要额外的条件语句替代的实际情况。
See Also
Why would someone use WHERE 1=1 AND <conditions> in a SQL clause?
回答by DubDub
For anyone who NEEDS the column name in the query for whatever reason (probably dynamic SQL), a nice alternative would be SELECT * FROM table WHERE column = column
对于出于任何原因(可能是动态 SQL)需要查询中的列名的任何人,一个不错的选择是 SELECT * FROM table WHERE column = column
This is very similar to WHERE 1=1
, however it includes the column name, which my solution required, and maybe a few others will require as well.
这与 非常相似WHERE 1=1
,但它包含列名,这是我的解决方案所需的,也许其他一些解决方案也需要。
回答by Steve Robbins
Everything or anything?
一切还是什么?
You could use a wildcard I guess.
我猜你可以使用通配符。
SELECT * FROM table WHERE column LIKE "%"
回答by mahmoodi.saeed
You can use column name itself in your query:
您可以在查询中使用列名本身:
SELECT * FROM TABLE WHERE COLUMN = COLUMN
for example:
例如:
SELECT * FROM Student WHERE YEAR = YEAR
or by using a Auxiliary parameter:
或使用辅助参数:
SELECT * FROM Student WHERE YEAR = (CASE WHEN @year IS NOT NULL THEN @year ELSE YEAR END)
so you can hold on "=" char
所以你可以坚持“=”字符
回答by Truth
SELECT * FROM table WHERE column = IF (? = '', column, ?);
回答by clattaclism
This is late but may be helpful to others
这是晚了,但可能对其他人有帮助
You can try this.
你可以试试这个。
where
isnull([column], '') = CASE WHEN @column IS NULL THEN isnull([column], '') ELSE @column END
回答by gazarsgo
Are you perhaps looking for the IN criteria operator?
您是否正在寻找 IN 条件运算符?
SELECT * from table where column in (1,2,3,4,5) or column in ('value', 'value2', 'value3');
回答by Olamide226
Well I had this same issue too and the following solved my problem:
好吧,我也遇到了同样的问题,以下解决了我的问题:
... where column = case when @variable = 'all' then column else @variable end
Keep it in mind that you must always send a default value, i set my default value as 'all'. So if i set
@variable = 'all'
, mysql reads it as :where column = column
which is the same thing aswhere 1=1
请记住,您必须始终发送默认值,我将默认值设置为“全部”。因此,如果我设置
@variable = 'all'
,mysql 将其读取为:where column = column
与where 1=1
回答by Warkaz
If this helps anyone... just to point out, if you have a problem such as SELECT * FROM something WHERE (can be specific or everything) such as filtering stuff,
you can use SELECT * FROM something as s WHERE (?1 = 0 OR ?1 = s.type = ?1)
0 here is a just predefined for all, so feel free to change, i needed this while using JPA repositories with hibernate for filtering. You can't do this medicinally as suggested by a previous answer because of the safety with prepared statements. Where the ?1 corresponds to :
如果这对任何人有帮助...只是指出,如果您遇到诸如 SELECT * FROM something WHERE(可以是特定的或所有内容)之类的问题,例如过滤内容,您可以使用SELECT * FROM something as s WHERE (?1 = 0 OR ?1 = s.type = ?1)
0 在这里是为所有人预定义的,所以感觉可以自由更改,我在使用带有休眠功能的 JPA 存储库进行过滤时需要这个。由于准备好的声明的安全性,您不能像以前的答案所建议的那样在医学上做到这一点。其中 ?1 对应于:
Page<Something> filterSometing(Long type,Pageable pageable);