Mysql: intvalue='1' 之间的区别且整数值=1
之间有区别吗
在 int
列上,执行 .. SELECT .. int = '1'
和 SELECT .. int = 1
?哪个会是首选?
On an int
column, is there difference between doing..
SELECT .. int = '1'
and SELECT .. int = 1
, and which would be preferred?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
int = 1
,因为int = '1'
在内部转换为int = cast('1' as int)
Use
int = 1
, becauseint = '1'
is internally converted toint = cast('1' as int)
不是 MySql 专家,但我想说第一个 (
int = '1'
) 会导致在比较之前将字符串转换为整数。因此,为了避免这种转换,我总是使用正确的数据类型。
此外,不保证转换成功(或给出正确的结果)。
Not a MySql expert, but I would say that the first one (
int = '1'
) would cause a conversion of the string to an integer before comparison.So to avoid this conversion, I would always go with the right data type.
Additionally, a conversion is not guaranteed to succeed (or give the correct results).
一个区别是,如果你的查询有一个可以为空的 var,你会得到这个
与
最后一个是无效的 SQL。第一个将为您提供所有空值(当然,如果没有,则可能什么也没有)。虽然我同意如果有人说应该在查询之前捕获它,但这是一个可以告诉你的区别:)
One difference is that if your query has a var that can be empty you'll get this
versus
And the last one is invalid SQL. The first one will give you all empty values (maybe nothing if there are none ofcourse). although I agree if one says it should be caught before the query, it is a difference that can byte you :)
第一个 (int = '1') 将字符串与整数进行比较(因此 mysql 自动将其转换)
秒 (int = 1) 将整数与整数字段进行比较(无需转换)。
所以首选第二个
The first one (int = '1') compares a string to an integer (so mysql automaticly converts this)
The seconds one (int = 1) compares an integer to an integer field (no conversion needed).
So the second one is preferred