多个字段的多条件排序的SQL语句怎么写?(急)

shines77 2003-05-19 06:14:09
有ID, 类别,时间,属性几个字段,

在整个记录序列中:
1,属性为1~5的排在最前面,其中是按时间从小到大排序,按类别排序次之;
2. 属性为0的排在中间,其中按类别,时间排序(类别优先,时间次之);
3. 属性为其他的排在最后面,其中按类别,时间排序。

这样的SQL语句该怎么写,我想用一条语句写出来,如果一条写出来效率不高,请告知效率更高的方法和原因?
...全文
1037 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
dgm8 2003-05-20
  • 打赏
  • 举报
回复
恭喜你解决了问题
shines77 2003-05-19
  • 打赏
  • 举报
回复
我自己用数组,经过几次排序搞定了,

谢谢大家!!
shines77 2003-05-19
  • 打赏
  • 举报
回复
ORDER BY and COMPUTE clauses to define the order of the final results or compute summary values are allowed only at the end of the UNION statement. They cannot be used within the individual queries that make up the UNION statement.

所以 funboy88(司令) 你说的还是实现不了:(
排序有问题
wsmagic 2003-05-19
  • 打赏
  • 举报
回复
帮你up一下
shines77 2003-05-19
  • 打赏
  • 举报
回复
好像Order By 语句有点乱,好像只认第一个,或者是没有生效,

或者是我的语句有错。。。。。
shines77 2003-05-19
  • 打赏
  • 举报
回复
OK 啦,多些,union all 的Select语句最好有()括起来,这样不会出错。
(select ......)
union all
(select ......)
union all
(select ......)

待会给分。
shines77 2003-05-19
  • 打赏
  • 举报
回复
试试
funboy88 2003-05-19
  • 打赏
  • 举报
回复
这有什么难度的


select ID, 类别,时间,属性 from 表 where 属性>=1 and 属性<=5 order by 时间,类别
union all
select ID, 类别,时间,属性 from 表 where 属性=0 order by 类别,时间
union all
select ID, 类别,时间,属性 from 表 where 属性<>0 and (属性<1 or 属性>5)order by 类别,时间



这样就能满足你的要求了

用union all的方法就是把多个查询结果组合成一个新的记录集
完全满足,最后一个的where语句根据你的实际数据再修改
try


shines77 2003-05-19
  • 打赏
  • 举报
回复
由于查询涉及三个表(From 部分),所以还是用数组记录下来,自己排序吧
麻烦是麻烦了点

如果分三次查询,效率实在太低
shines77 2003-05-19
  • 打赏
  • 举报
回复
好像不行。。。
shines77 2003-05-19
  • 打赏
  • 举报
回复
慕白兄:
谢谢你的提示,我的是Access,我试了一下,好像不支持 case when,我看Access有类似语句吗。。。。
cpp2017 2003-05-19
  • 打赏
  • 举报
回复
写错了,
不是id而是属性
以上是sqlserver的写法,不知道你的数据库是什么?

</慕白兄>
shines77 2003-05-19
  • 打赏
  • 举报
回复
我觉得一条语句效率高啊,如果能够只做一次Select那就更好了,不过好像不太可能

一条语句是比三次搜索效率高的,应该

tonnycncn(托尼) :结果最好一次输出,
cpp2017 2003-05-19
  • 打赏
  • 举报
回复
Select xxxx, (case when id>=1 and id<=5 then -1 else id end )
as sortfield order by

sortfield,case when id>=1 and id<=5 then 时间 else 类别 end ,
case when id>=1 and id<=5 then 类别 else 时间 end
</慕白兄>
tonnycncn 2003-05-19
  • 打赏
  • 举报
回复
这样的需求,用一种语句不好。难度不说,效率也不高。
建议分三次搜索。结果依次输出。
cpp2017 2003-05-19
  • 打赏
  • 举报
回复
没有这样的排法



</慕白兄>
shines77 2003-05-19
  • 打赏
  • 举报
回复
最笨的方法就是写三条SQL语句,三次分别查询1,2,3

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧