Home > Database > Mysql Tutorial > mysql中null值在表达式计算时的影响_MySQL

mysql中null值在表达式计算时的影响_MySQL

WBOY
Release: 2016-06-01 13:37:39
Original
967 people have browsed it

bitsCN.com

mysql中null值在表达式计算时的影响

 

mysql中 当任意类型字段和null值进行计算时得到的结果依然为null 值

例如  1+ null =null  

 

'a'+null =null

 

有时候需要自动转换null值为整型来进行表达式计算.

 

尝试使用CONVERT 和 CAST 函数 强行转换 null值为整型 ..失败.

 

最后无奈还是使用了 if 语句来解决这个问题

 

例子如下:  if (a is null,b,b+a) as c   意思为 当  a字段为null值时,只计算b字段的值 取别名为c  否则 取 b字段和a字段的值相加 取别名为c

 

a        b

 

null     1

 

1       1 

 

select if (a is null,b,b+a) as c from table 

 

最后得到结果为

 

c

 

1

 

2

 

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template