SQL 中的 LIKE 与整数
我可以用整数 的 LIKE
语句替换 =
语句吗?
例如:以下是同一件事:
select * from FOOS where FOOID like 2
// and
select * from FOOS where FOOID = 2
我更喜欢使用 LIKE
而不是 =
因为当我没有 FOOID 过滤器时我可以使用 %
...
SQL Server 2005。
编辑1 @Martin
Can I replace the =
statement with the LIKE
one for the integers ?
by eg. are the following the same thing:
select * from FOOS where FOOID like 2
// and
select * from FOOS where FOOID = 2
I'd prefer to use LIKE
instead of =
because I could use %
when I have no filter for FOOID...
SQL Server 2005.
EDIT 1 @Martin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
应避免,因为它会导致双方隐式转换为
varchar
并意味着索引无法用于满足查询。计划(注意估计成本)
查看成本差异的另一种方法是添加
SET STATISTICS IO ON
您会看到第一个版本返回类似的内容
第二个版本返回
这是因为在此索引上查找所需的读取与索引深度成正比,而扫描所需的读取与数量成正比索引中的页面数。表格越大,这两个数字之间的差异就会越大。您可以通过运行以下命令来查看这两个数字。
should be avoided as it will cause both sides to be implicitly cast as
varchar
and mean that an index cannot be used to satisfy the query.Plans (notice the estimated costs)
Another way of seeing the difference in costs is to add
SET STATISTICS IO ON
You see that the first version returns something like
The second version returns
This is beacuse the reads required for the seek on this index are proportional to the index depth whereas the reads required for the scan are proportional to the number of pages in the index. The bigger the table gets the larger the discrepancy between these 2 numbers will become. You can see both of these figures by running the following.
使用
CASE
语句将输入字符串转换为整数。将通配符%
转换为NULL
。与将整个 int 列隐式转换为字符串相比,这将提供更好的性能。Use a
CASE
statement to convert an input string to an integer. Convert the wildcard%
to aNULL
. This will give better performance than implicitly converting the entire int column to string.是的,你可以直接使用它:
否则
整数将被隐式转换为字符串。
请注意,这两个条件都不可控制,即无法在 fooid 上使用索引。这总是会导致全表扫描(或
fooid
上的全索引扫描)。Yes, you can just use it:
or
Integers will be implicitly converted into strings.
Note that neither of these condition is sargable, i. e. able to use an index on
fooid
. This will always result in a full table scan (or a full index scan onfooid
).这是一个迟来的评论,但我想也许其他人正在寻找同样的东西,所以当我能够找到解决方案时,我想我应该在这里分享:)
问题的简短描述:
我遇到的问题是能够使用通配符来对抗整数数据类型。我正在使用 SQL Server,因此我的语法适用于 SQL Server。我有一列显示部门编号,我想从下拉菜单中从我的页面传递一个变量。还有一个“全部”选项,在这种情况下我想传递“%”作为参数。我正在使用这个:
当我传递一个数字时它起作用,但不适用于
%
因为 sql server 隐式地将@DepartmentID
转换为 int (因为我的 deptNo 是 int 类型)所以我转换了 deptNo 并解决了这个问题:
当我传递像 4 这样的数字和当我传递
%
时,这个都适用。This is a late comment but I thought maybe some other people are looking for the same thing so as I was able to find a solution for this, I thought I should share it here:)
A short description of the problem:
the problem I had was to be able to use the wild card foe integer data types. I am using SQL Server and so my syntax is for SQL Server. I have a column which shows department number and I wanted to pass a variable from my page from a drop down menu. There is an 'All' option as well which in that case I wanted to pass '%' as the parameter. I was using this:
It was working for when I pass a number but not for
%
because sql server implicitly converts the@DepartmentID
to int (as my deptNo is of type int)So I casted the deptNo and that fixed the issue:
This one works for both when I pass a number like 4 and when I pass
%
.使用 NULL 作为参数值而不是 % 作为通配符条件
Use NULL as the parameter value instead of % for your wildcard condition