---
title: "Mysql coalesce() 函数认识和用法"
author: "wangning8075"
date: "2017-03-29T17:01:08Z"
tags: ["MySQL"]
categories: ["数据库"]
slug: "mysql-coalesce"
draft: false
lastmod: "2017-03-29T17:01:08Z"
id: 2366512
url: "https://wangning8075.iteye.com/blog/2366512"
---

coalesce认识：返回参数中的第一个非空表达式（从左向右)

类似于oracle的nav()函数，但mysql中没有，可以通过此函数替代，coalesce相比nvl优点是，coalesce中参数可以有多个，而nvl()中参数就只有两个。当然，在oracle中也可以使用 case when....then....else......end（比较强悍，case when 后可以跟表达式。

 

使用方法：

 

```
select coalesce(a,b,c) from temp
```

 解释：

 

如果a!=null,则返回a，如果a==null,则返回b，如果b==null,则返回c；如果a b c 都为null ，则返回为null