排序中新闻标题为空的排在前,其它按更新时间倒序排列。 select * from news order by isnull(newstitle),newsdate desc一样不能实现 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 select * from news order by case when newstitle is null then 0 else 1 end,newsdate desc -- 创建测试环境if exists(select 1 from [tempdb]..[sysobjects] where id=object_id('[tempdb]..[#]'))drop table [tempdb]..[#]gocreate table #( ID int identity,newstitle varchar(20),newsdate datetime)insert #(newstitle,newsdate)select '新闻1',getdate() unionselect '新闻2','2005-1-2' unionselect '新闻2','2005-1-3' unionselect '','2006-3-2' unionselect null,'2007-3-1'-- 测试select * from # order by newstitle,newsdate descselect * from # order by (case newstitle when null then 0 else 1 end),newsdate desc--删除测试环境drop table #--结果/*ID newstitle newsdate ----------- -------------------- ---------------------------------1 NULL 2007-03-01 00:00:00.0002 2006-03-02 00:00:00.0003 新闻1 2005-06-27 13:11:50.6535 新闻2 2005-01-03 00:00:00.0004 新闻2 2005-01-02 00:00:00.000(所影响的行数为 5 行)ID newstitle newsdate ----------- -------------------- -------------------------------- 1 NULL 2007-03-01 00:00:00.0002 2006-03-02 00:00:00.0003 新闻1 2005-06-27 13:11:50.6535 新闻2 2005-01-03 00:00:00.0004 新闻2 2005-01-02 00:00:00.000(所影响的行数为 5 行)*/ 写错了,应加上set ansi_nulls off-- 创建测试环境if exists(select 1 from [tempdb]..[sysobjects] where id=object_id('[tempdb]..[#]'))drop table [tempdb]..[#]gocreate table #( ID int identity,newstitle varchar(20),newsdate datetime)insert #(newstitle,newsdate)select '新闻1',getdate() unionselect '新闻2','2005-1-2' unionselect '新闻2','2005-1-3' unionselect '','2006-3-2' unionselect null,'2007-3-1'-- 测试set ansi_nulls offselect * from # order by newstitle,newsdate descselect * from # order by (case newstitle when null then 0 else 1 end),newsdate desc--删除测试环境drop table #--结果/*ID newstitle newsdate ----------- -------------------- ---------------------------------1 NULL 2007-03-01 00:00:00.0002 2006-03-02 00:00:00.0003 新闻1 2005-06-27 13:11:50.6535 新闻2 2005-01-03 00:00:00.0004 新闻2 2005-01-02 00:00:00.000(所影响的行数为 5 行)ID newstitle newsdate ----------- -------------------- -------------------------------- 1 NULL 2007-03-01 00:00:00.0002 2006-03-02 00:00:00.0003 新闻1 2005-06-27 13:11:50.6535 新闻2 2005-01-03 00:00:00.0004 新闻2 2005-01-02 00:00:00.000(所影响的行数为 5 行)*/ 我的数据库是 access 的,调不好。 ASP操作SQL Server数据库中的问题:“库、NAME、库中文件、表”之间的关系是怎样的?请看例子: 建立存储过程出现问题 问一个sql分页排序问题 Sql server2008安装问题 请各位介绍几本容易上手的SQL书籍!在线等!谢谢! 有这样一串字符,如何截取 求一简单查询!!50分~ 不能用存储过程建立数据吗? 送分了!!一个关于LotusScript的小问题! insert into table(varchar500) value ('一个长度大于256的字符串')为什么会被自动截断? 数据库长时间不是用,连接自动关闭,如何解决 交叉表问题,不知如何下手好~`请各位指教
if exists(select 1 from [tempdb]..[sysobjects] where id=object_id('[tempdb]..[#]'))
drop table [tempdb]..[#]
go
create table #
(
ID int identity,newstitle varchar(20),newsdate datetime
)
insert #(newstitle,newsdate)
select '新闻1',getdate() union
select '新闻2','2005-1-2' union
select '新闻2','2005-1-3' union
select '','2006-3-2' union
select null,'2007-3-1'-- 测试
select * from # order by newstitle,newsdate desc
select * from # order by (case newstitle when null then 0 else 1 end),newsdate desc--删除测试环境
drop table #--结果
/*
ID newstitle newsdate
----------- -------------------- ---------------------------------
1 NULL 2007-03-01 00:00:00.000
2 2006-03-02 00:00:00.000
3 新闻1 2005-06-27 13:11:50.653
5 新闻2 2005-01-03 00:00:00.000
4 新闻2 2005-01-02 00:00:00.000(所影响的行数为 5 行)ID newstitle newsdate
----------- -------------------- --------------------------------
1 NULL 2007-03-01 00:00:00.000
2 2006-03-02 00:00:00.000
3 新闻1 2005-06-27 13:11:50.653
5 新闻2 2005-01-03 00:00:00.000
4 新闻2 2005-01-02 00:00:00.000(所影响的行数为 5 行)
*/
-- 创建测试环境
if exists(select 1 from [tempdb]..[sysobjects] where id=object_id('[tempdb]..[#]'))
drop table [tempdb]..[#]
go
create table #
(
ID int identity,newstitle varchar(20),newsdate datetime
)
insert #(newstitle,newsdate)
select '新闻1',getdate() union
select '新闻2','2005-1-2' union
select '新闻2','2005-1-3' union
select '','2006-3-2' union
select null,'2007-3-1'-- 测试
set ansi_nulls off
select * from # order by newstitle,newsdate desc
select * from # order by (case newstitle when null then 0 else 1 end),newsdate desc--删除测试环境
drop table #--结果
/*
ID newstitle newsdate
----------- -------------------- ---------------------------------
1 NULL 2007-03-01 00:00:00.000
2 2006-03-02 00:00:00.000
3 新闻1 2005-06-27 13:11:50.653
5 新闻2 2005-01-03 00:00:00.000
4 新闻2 2005-01-02 00:00:00.000(所影响的行数为 5 行)ID newstitle newsdate
----------- -------------------- --------------------------------
1 NULL 2007-03-01 00:00:00.000
2 2006-03-02 00:00:00.000
3 新闻1 2005-06-27 13:11:50.653
5 新闻2 2005-01-03 00:00:00.000
4 新闻2 2005-01-02 00:00:00.000(所影响的行数为 5 行)
*/