Home > Database > Mysql Tutorial > 关于order by的一些用法

关于order by的一些用法

WBOY
Release: 2016-06-07 16:07:32
Original
2013 people have browsed it

1. order by 中关于null的处理 缺省处理:Oracle在order by时认为null是最大值, 所以如果是asc升序则排在最后, desc降序则排在最

1. order by 中关于null的处理

缺省处理:Oracle在order by时认为null是最大值, 所以如果是asc升序则排在最后, desc降序则排在最前.

我们可以使用nulls first或者nulls last来控制null的位置

将null放在最前:select * from student order by name asc nulls first

将null放在最后:select * from student order by name desc nulls last

2. 几种排序的写法:

单列升序:select name from student order by name; (默认升序,即使不写asc)
单列降序:select name from student order by name desc;
多列升序:select id, name from student order by id, name;
多列混合排序:select id, name from student order by id desc, name asc; (先按id降序,,如果id相同则按name升序)

3. 强制某列排第一:

select * from student order by decode(id, 3, 1, 2), id;  // id为3的值排第一

本文永久更新链接地址

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template