我在db中有一列包含以下值之一:1 2 3 4
我想以这种方式按照这个值订购:
mix between 1 & 3 then 2 then 4.
表:服务
列:
name,service_type
服务类型可以是1 2 3 4,我想显示订单服务1和3然后2然后4
任何想法??
亲切的问候
答案 0 :(得分:1)
也许是这样的,使用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 |
+------+------+----------+
如果mix
不够特殊,请转到
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