Mysql coalesce()函数认识和用法

Mysql coalesce()函数认识和用法

coalesce()解释:返回参数中的第一个非空表达式(从左向右);

鉴于在mysql中没有nvl()函数, 我们用coalesce()来代替。

coalesce相比nvl优点是,coalesce中参数可以有多个,而nvl()中参数就只有两个。

当然,在oracle中也可以使用 case when....then....else......end

(比较强悍,case when 后可以跟表达式)。
  www.2cto.com 
使用示例:a,b,c三个变量。

Sql代码 
select coalesce(a,b,c); 

如果a==null,则选择b;如果b==null,则选择c;如果a!=null,则选择a;如果a b c 都为null ,则返回为null(没意义)。

猜你喜欢

转载自schooltop.iteye.com/blog/2107097