自定義順序排序
阿新 • • 發佈:2019-01-06
--sql code
create table tb
(
GSIZE varchar(10)
)
insert into tb(GSIZE) values('M')
insert into tb(GSIZE) values('L')
insert into tb(GSIZE) values('S')
insert into tb(GSIZE) values('XL')
insert into tb(GSIZE) values('3XL')
insert into tb(GSIZE) values('2XL')
insert into tb(GSIZE) values('4XL')
--size:關於物料的size,例如衣服,鞋之類的產品,如衣服size(按大到小排序應為):S,M,L,XL,2XL,3XL,4XL
--我sql 查詢時怎樣做排序呢:不知道有沒有如到這種問題呢?
select * from tb order by GSIZE
select * from tb
order by charindex(','+GSIZE+',',',S,M,L,XL,2XL,3XL,4XL,')
select * from tb
order by
case when charindex('S',GSIZE)>0 then 1
when charindex('M',GSIZE)>0 then 2
when charindex('L',GSIZE)>0 then 3
end,
len(GSIZE),
GSIZE
/**
GSIZE
----------
S
M
L
XL
2XL
3XL
4XL
(所影響的行數為 7 行)
**/
http://topic.csdn.net/u/20100111/17/ccc472de-0964-408f-bc49-93ce8aeb7597.html