select '星期' + y,ISNULL(人数,0) FROM
(
select 1 x,'一' y
union all
select 2,'二'
union all
....
select 7,'日')
) b
LEFT join tb a
on charindex(y,星期)>0
order by x
select * from tb
order by case 星期 when '星期一' then 1
when '星期二' then 2
when '星期三' then 3
when '星期四' then 4
when '星期五' then 5
when '星期六' then 6
when '星期日' then 7
end
表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