实现排序,并将null记录放在前面或者最后,
oracle直接使用 nulls first
、nulls last
即可,
mysql实现的话,以下两种方式都可以
方式一
nulls first:
order by IF(ISNULL(field),0,1);
nulls last:
order by IF(ISNULL(field),1,0);
方式二
nulls first:
order by NOT ISNULL(field);
nulls last:
order by ISNULL(field);
另外,上面方式非null记录是没有进行排序的,直接在后面加上该字段即可实现,即:
order by ISNULL(field), field ASC|DESC;
order by IF(ISNULL(field),1,0),field ASC|DESC;