就像'%'不接受 NULL 值
我有一个根据用户输入构建的查询(通过 html 表单传递)。它看起来像(简单的示例):
Select * From [table] Where [table].[column] like '<parameter>'
此参数可能是可选的,因此如果用户将相应的输入字段留空,我会传递 %。它工作得很好,直到我遇到 NULL 值。我知道 '%' 匹配符号不为空,但在这种情况下我想将 NULL 视为空字符串。
我应该怎么办?当用户留空输入时更改查询(如何?)或传递另一个符号?
谢谢。
PS。这是现有系统的真正问题,我知道它远非最佳解决方案,但我必须处理它。
I have query that is build from user's inputs ( passed via html form). It looks like (simple example):
Select * From [table] Where [table].[column] like '<parameter>'
This parameter may be optional so if user left corresponding input field empty I pass %. It worked fine until I encountered NULL values. I understand that '%' match symbols not null, but i would like to consider NULL as empty string in this case.
What should I do? Change query (how?) or pass another symbol(s) when user left empty input?
Thanks.
PS. It's real problem from existing system and i know it's far from optimal solution, but i have to deal with it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以使用
coalesce
将null视为空字符串:在SQL Server上,您还可以使用
IsNull
:You can use
coalesce
to treat null like an empty string:On SQL Server, you can also use
IsNull
:就像魅力一样工作
Works like a charm
我认为这可能有效:
I think this might work:
好吧,怎么样
...这样当您传递 '%' 时,您将获得所有行,否则它就像您现在拥有它一样。
Well, how about
...so that when you pass '%', you get all the rows back, otherwise it works like you have it at the moment.
怎么样..
所以这将采用您的实际列值,或者如果它为空,则为空字符串并将其与您的参数进行比较,假设您使用的是 MS SQL 服务器。
How about..
So this will take your actual column value, or if that's null an empty string and compare it against your parameter, assuming you're using MS SQL server..
发表两点声明!如果用户没有传递参数user:
Make two statements! If the user passed no parameter user:
基于 Where 子句中的索引 问题这种方法
是:
如果您在[列]上使用了索引,由于COALESCE功能,SQL Server无法使用您的索引,这意味着您已经浪费了索引
并且
这种方法的问题
是:
如果[表] .[column] 为 null 那么代码将如下所示:
并且无论Where 子句的第二部分如何([table].[column] 为 null)
评估的结果将为 UNKNOWN 并且 SQL Server 过滤器会记录出来。
NULL OR True = UNKNOWN
NULL OR False = UNKNOWN
因此,这是优化且包含 null 的方法:
Based On Index in Where Clause Issue With This Approach
Is :
If you have used an Index on your [column], because of the COALESCE function,SQL Server cant use your index,it means that you've wasted your index
AND
Issue With This Approach
Is :
If the [table].[column] is null then the code will be like this :
and regardless of second part of Where clause ([table].[column] is null)
the result of evaluation will be UNKNOWN and the SQL Server filter that record out.
NULL OR True = UNKNOWN
NULL OR False = UNKNOWN
So this is the optimized and null included approach :
美好的一天,使用这个解决方案,我认为它是解决方案的混合:
如果您只想从参数开始,请删除第一个“%”和加号
我希望您发现它有用
Good day, use this solution, I think it's a mixture of solutions:
If you just want to start with the parameter, removes the first '%' and the plus sign
I hope you find it useful
我想要类似的东西,实际上能够找到“%”(全部),包括 null 或输入时的特定值。
对于 Oracle,
isnull
不起作用,在我的情况下,COALESCE
也不起作用。我在 where 子句中使用了这个选项:
希望它对其他人有用。
I wanted something similar, to actually be able to find '%' (all) including null or a specific value when input.
For Oracle
isnull
does not work and in my caseCOALESCE
neither.I Used this option in the where clause:
Hope it works for others.
对于 SQLITE
For SQLITE