数据库中有一列包含以下值之一:1 2 3 4
我想按此值订购:
mix between 1 & 3 then 2 then 4.
表:服务
柱:
名称,服务类型
服务类型可以是1 2 3 4,我想显示订单服务1和3,然后是2,然后是4
知道吗??
亲切的问候
最佳答案
可能是这样的,使用case/when
:
create table t7
( name varchar(20) not null,
aNum int not null
);
insert t7 (name,aNum) values ('a',2),('z',3),('n',3),('q',4),('t',1),
('q',2),('w',3),('e',3),('r',4),('t',1),('y',2);
select name,aNum,
CASE WHEN aNum in (1,3) THEN 1 else 2 end as theOrder
from t7
order by theOrder;
+------+------+----------+
| name | aNum | theOrder |
+------+------+----------+
| z | 3 | 1 |
| n | 3 | 1 |
| t | 1 | 1 |
| t | 1 | 1 |
| w | 3 | 1 |
| e | 3 | 1 |
| a | 2 | 2 |
| r | 4 | 2 |
| q | 2 | 2 |
| q | 4 | 2 |
| y | 2 | 2 |
+------+------+----------+
如果这还不够特别,那么
select name,aNum from
( select name,aNum from t7 where aNum in (1,3) order by rand()) x1
union all
select name,aNum from
( select name,aNum from t7 where aNum in (2,4) order by rand()) x2