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

时间:2022-06-01 20:28:44
有ID, 类别,时间,属性几个字段,

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

这样的SQL语句该怎么写,我想用一条语句写出来,如果一条写出来效率不高,请告知效率更高的方法和原因?

17 个解决方案

#1


最笨的方法就是写三条SQL语句,三次分别查询1,2,3

#2


没有这样的排法



</慕白兄>

#3


这样的需求,用一种语句不好。难度不说,效率也不高。
建议分三次搜索。结果依次输出。

#4


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 
</慕白兄>

#5


我觉得一条语句效率高啊,如果能够只做一次Select那就更好了,不过好像不太可能

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

tonnycncn(托尼) :结果最好一次输出,

#6


写错了,
不是id而是属性
以上是sqlserver的写法,不知道你的数据库是什么?

</慕白兄>

#7


慕白兄:
    谢谢你的提示,我的是Access,我试了一下,好像不支持 case when,我看Access有类似语句吗。。。。

#8


好像不行。。。

#9


由于查询涉及三个表(From 部分),所以还是用数组记录下来,自己排序吧
麻烦是麻烦了点

如果分三次查询,效率实在太低

#10


这有什么难度的


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


#11


试试

#12


OK 啦,多些,union all 的Select语句最好有()括起来,这样不会出错。
(select ......)
union all
(select ......)
union all
(select ......)

待会给分。

#13


好像Order By 语句有点乱,好像只认第一个,或者是没有生效,

或者是我的语句有错。。。。。

#14


帮你up一下

#15


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(司令)  你说的还是实现不了:(
排序有问题

#16


我自己用数组,经过几次排序搞定了,

谢谢大家!!

#17


恭喜你解决了问题

#1


最笨的方法就是写三条SQL语句,三次分别查询1,2,3

#2


没有这样的排法



</慕白兄>

#3


这样的需求,用一种语句不好。难度不说,效率也不高。
建议分三次搜索。结果依次输出。

#4


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 
</慕白兄>

#5


我觉得一条语句效率高啊,如果能够只做一次Select那就更好了,不过好像不太可能

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

tonnycncn(托尼) :结果最好一次输出,

#6


写错了,
不是id而是属性
以上是sqlserver的写法,不知道你的数据库是什么?

</慕白兄>

#7


慕白兄:
    谢谢你的提示,我的是Access,我试了一下,好像不支持 case when,我看Access有类似语句吗。。。。

#8


好像不行。。。

#9


由于查询涉及三个表(From 部分),所以还是用数组记录下来,自己排序吧
麻烦是麻烦了点

如果分三次查询,效率实在太低

#10


这有什么难度的


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


#11


试试

#12


OK 啦,多些,union all 的Select语句最好有()括起来,这样不会出错。
(select ......)
union all
(select ......)
union all
(select ......)

待会给分。

#13


好像Order By 语句有点乱,好像只认第一个,或者是没有生效,

或者是我的语句有错。。。。。

#14


帮你up一下

#15


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(司令)  你说的还是实现不了:(
排序有问题

#16


我自己用数组,经过几次排序搞定了,

谢谢大家!!

#17


恭喜你解决了问题