---
title: "mysql一个字段为空时使用另一个字段排序"
author: "start_p"
date: "2015-04-20T18:14:53Z"
tags: []
categories: ["数据库"]
slug: "mysql"
draft: false
lastmod: "2015-04-20T18:15:58Z"
id: 2204246
url: "https://guard.iteye.com/blog/2204246"
---

表中有两个日期字段createDate，updateDate。其中updateDate可以为空，要求使用updateDate排序，如果updateDate为空则使用createDate排序，结果要顺序排下来。

    按照常规方法：

```
select * from table order by update desc
```

 这样的结果是为空的数据排在了最下面，不符合要求。

  方法二：

```
select * from table order by updateDate desc, createDate desc
```

 这样排的结果是先按updateDate排序，updateDate为空的排在最下面，然后按createDate排序，这样也不符合要求。

  方法三：

```
select * from table order by IFNULL(updateDate, createDate) desc
```

 这种排序的结果是正确的，用ifnull函数判断updateDate如果为空的话就使用createDate排