代码改变世界

mysql null排序

2012-05-08 09:07  java环境变量  阅读(206)  评论(0编辑  收藏  举报

对null值排序


select * from t1 order by sname desc;

+-----+-----+--------+
| sid | sno | sname  |
+-----+-----+--------+
| 103 | 203 | 徐文宇 |
| 104 | 204 | kk     |
| 101 | 201 | NULL   |
| 103 | 203 | NULL   |
+-----+-----+--------+

 

select * from t1 order by if(sname is null,0,1),sname desc;

+-----+-----+--------+
| sid | sno | sname  |
+-----+-----+--------+
| 101 | 201 | NULL   |
| 103 | 203 | NULL   |
| 103 | 203 | 徐文宇 |
| 104 | 204 | kk     |