我从一个同事那里继承了一些代码,我注意到一些代码不一致的方式,例如,以下代码之间有任何功能上的差异:
sum(case when (elephants = 0)then 1 else 0 end),
sum(case when (elephants = '0')then 1 else 0 end),
sum(case when (elephants IN (0))then 1 else 0 end),
sum(case when (elephants IN ('0'))then 1 else 0 end);如果在查找单个值时使用单个qutoes或IN vs =之间没有功能上的差异,那么还有什么其他原因可以解释它(除了草率的代码之外)?
发布于 2012-11-02 23:12:56
x IN (a, b, c)的意思是x = a OR x = b OR x = c。当IN列表包含单个项目时,x IN (a)仅表示x = a。
至于0和'0'的区别,前者是整数,后者是字符串。后者可以转换为整数,因此当elephants也是整数时,elephants = 0和elephants = '0'也会测试相同的东西。
这四个之间并没有什么真正的区别。
发布于 2012-11-02 23:12:06
=表示单个值,但IN在一个集合中可以有多个值。
=示例
a = b但是对于IN来说,不是编写多个OR
a = 1 OR a = 2 or a = 3你可以把它写成
a IN (1,2,3)关于单引号,如果列的数据类型是numeric,则服务器会自动将字符串值解析为数值。
发布于 2012-11-02 23:14:20
简而言之:
= 0 //equal to the number zero
= '0' //equal to the string "0"
IN (0) //appears in the list in parenthesis. In this case, the single-item list of numerical zero
IN ('0') //appears in the list in parenthesis. In this case, the single-item list of string "0"https://stackoverflow.com/questions/13197934
复制相似问题