---
title: "mysql 统计时 DISTINCT 的字段有  null值解决办法"
author: "不念书的小子"
date: "2012-06-19T14:18:22Z"
tags: ["mysql 统计时 DISTINCT 的字段有  null值解决办法"]
categories: ["数据库"]
slug: "mysql-distinct-null"
draft: false
lastmod: "2012-06-19T14:19:42Z"
id: 1563930
url: "https://tompig.iteye.com/blog/1563930"
---

coalesce函数表示可以返回参数中的第一个非空表达式，当你有N个参数时选取第一个非空值（从左到右）。

        实例一：

              select coalesce（null,"carrot","apple"）

          返回结果：carrot

       实例二：

              select coalesce(1,"carrot","apple")

           返回结果：1

 

用法：

如果user\_id 有为空的情况时可以使用：

 

SELECT  \*,count( DISTINCT COALESCE(user\_id,'NULL')) as  visit\_count  FROM t