日期:2014-05-18  浏览次数:20531 次

求问一sql按照星期排序
查询出的结果数据如下:
星期           人数
星期二       1
星期六       2
星期日       3
星期三       4
星期四       5
星期五       6
星期一       7

怎么样按照星期一   星期二...星期日的顺序来排列?

------解决方案--------------------
如何让ORDER BY按指定的顺序排序

表a里有个列叫Type,是商品类别,就3种情况:S,A,B,如下:
id name type
1 一班 S
2 五班 A
3 三班 B
4 四班 B
5 二班 A
6 六班 S
现在我需要按照‘S’,‘A’,‘B’的顺序排序,如下:
1 一班 S
6 六班 S
5 二班 A
2 五班 A
3 三班 B
4 四班 B

SELECT *
FROM tbl_test
ORDER BY "@#$$%#$%@$@#$@$@#@$这里应该咋写? "

select * from a where type= 'S ' union all
select * from a where type= 'A ' union all
select * from a where type= 'B '
select id , name ,type
from a
order by case type when 'S ' then 1
when 'A ' then 2 when 'B ' then 3 else 4 end (如果对ID排序,则加最后加,id)
select id , name ,type
from (select *, case type when 'S ' then 1 when 'A ' then 2 else 3 end as seq from a) X
order by seq
上诉对ID列没有进行排序,如果在上诉基础上对ID再进行排序。
select id , name ,type(假设有列id ,name, type)
from (select *, case type when 'S ' then 1 when 'A ' then 2 else 3 end as seq from a) X
order by seq,id
1 一班 S
6 六班 S
2 五班 A
5 二班 A
3 三班 B
4 四班 B


------解决方案--------------------
order by
replace(replace(replace(replace(replace(replace(replace(replace(星期, '一 ', '1 '), '二 ', '2 '), '三 ', '3 '), '四 ', '4 '), '五 ', '5 '), '六 ', '6 '), '日 ', '7 '), '星期 ', ' ')

这样不直观或者

select a.* from tb a
inner join
(
select 1 x, '一 ' y
union all
select 2, '二 '
union all
....
select 7, '日 ')
) b
on y charindex(y,星期)> 0
order by x