各种数据库产品数据按某字段Order by时null的位置问题
来源:互联网 发布:猴王水果竞猜php源码 编辑:程序博客网 时间:2024/04/30 01:15
oracle、mssql、mysql三种数据库产品在order by时: 参见:
Oracle在Order by时缺省认为null是最大值,所以如果是ASC升序则排在最后,DESC降序则排在最前面;
mssql和mysql在order by时缺省认为null是最小值,所以如果是ASC升序则排在最前面,DESC降序则排在最后;
对任意的数据库产品,想要在按某字段排序时(不管是升序还是降序),对字段值为null的记录的出现顺序做任意的安排,可以使用以下的通用做法:
ORDER BY CASE WHEN Col Is NULL Then 1/0 Else 0/1 End,Col [asc/desc]
Example 1:
oracle order by ,按Col升序,但是Col为null的排最前:
Example 2:
mssql/mysql order by,按Col升序,但是Col为null的排最后:
http://sqlblog.com/blogs/denis_gobo/archive/2007/10/19/3048.aspx
Sort Values Ascending But NULLS Last
This is a frequent request in newsgroups and fora. People want to sort the column in ascending order but don't want the NULLS at the beginning.
Oracle has this syntax: ORDER BY ColumnName NULLS LAST;
SQL Server does not have this. But there are 2 ways to do this. The first one is by using case and the second one by using COALESCE and the maximum value for the data type in the order by clause.
The 2 approaches with a datetime data type
DECLARE @Temp table(Col datetime)
INSERT INTO @Temp VALUES(getdate())
INSERT INTO @Temp VALUES('2007-10-19 09:54:03.730')
INSERT INTO @Temp VALUES('2006-10-19 09:54:03.730')
INSERT INTO @Temp VALUES('2005-10-19 09:54:03.730')
INSERT INTO @Temp VALUES('2006-10-19 09:54:03.730')
INSERT INTO @Temp VALUES('2004-10-19 09:54:03.730')
INSERT INTO @Temp VALUES(NULL)
INSERT INTO @Temp VALUES(NULL)
SELECT *
FROM @Temp
ORDER BY COALESCE(Col,'9999-12-31 23:59:59.997')
SELECT *
FROM @Temp
ORDER BY CASE WHEN Col Is NULL Then 1 Else 0 End, Col
The 2 approaches with an integer data type
DECLARE @Temp table(Col int)
INSERT INTO @Temp VALUES(1)
INSERT INTO @Temp VALUES(555)
INSERT INTO @Temp VALUES(444)
INSERT INTO @Temp VALUES(333)
INSERT INTO @Temp VALUES(5656565)
INSERT INTO @Temp VALUES(3)
INSERT INTO @Temp VALUES(NULL)
INSERT INTO @Temp VALUES(NULL)
SELECT *
FROM @Temp
ORDER BY COALESCE(Col,'2147483647')
SELECT *
FROM @Temp
ORDER BY CASE WHEN Col Is NULL Then 1 Else 0 End, Col
- 各种数据库产品数据按某字段Order by时null的位置问题
- 各种数据库产品数据按某字段Order by时null的位置问题
- order by 语句对null字段的默认排序
- order by 语句对null字段的默认排序
- order by 语句对null字段的默认排序
- order by 语句对null字段的默认排序
- MySQL中order by语句对null字段的排序
- order by 语句对null字段的默认排序
- order by 语句对null字段的默认排序
- MySQL中order by语句对null字段的排序
- MySQL中order by语句对null字段的排序
- 转自:http://m.blog.csdn.net/article/details?id=6554168 在使用order by语句进行查询结果排序时,不同的数据库对于被排序字段数据行为null的情况
- Hibernate的order by遇到null致数据丢失的问题
- mysql数据库order by语句 null 处理
- oracle 数据库 order by排序的时候如何控制null列数据是排在前面还是后面
- 笔记三:关于在Windows环境下mysql数据库数据ORDER BY排序的问题
- MySQL在排序时(Order by), Null值在什么位置?
- 数据库分页时order by排序不唯一,分页出现重复数据问题
- Using XMLHttpRequest
- 使用FxCop做代码检查和优化
- nsIObserverService
- xen虚拟机的启动(引导)方式
- Ambarella公司申请首次公开招股
- 各种数据库产品数据按某字段Order by时null的位置问题
- java.net.BindException: Address already in use: JVM_Bind
- Visual Studio的配色方案,支持vs2005、vs2008、vs2010
- Visual Studio 2008 每日小窍门 【持续发布ing~】 Tips:028
- [WaterDrops]std::map中判断并删除元素
- 自动设置ip的dns的window脚本
- 2011年,编程语言使用排行榜
- 做别人的尾灯
- MyEclispe 中如何编辑自带的Libraries,修改和添加jar包