作者:xiaozhao | 来源:互联网 | 2023-05-18 03:57
一个人员表:id,name,type。其中type表示分类用的,比如有1,2,3的。现在想查:type1的人放在前面,剩下的放在后面。比如:id1王五1id2张三1
一个人员表:
id,name,type。其中type表示分类用的,比如有1,2,3的。
现在想查:
type=1的人放在前面,剩下的放在后面。
比如:
id1 王五 1
id2 张三 1
id3 李四 3
id4 赵六 1
id5 王麻子 2
查询的结果要
id1 王五 1
id2 张三 1
id4 赵六 1
id5 王麻子 2
id3 李四 3
至于不是1的排序随便。
13 个解决方案
可能这样说还是不太清楚,
其实就是在一个表中查询指定条件的结果,然后 不符合指定条件的那些数据就跟在 符合条件的那些数据后面。
group by 可能不行,因为 条件不一定是type=多少,也可能是 type in (1,2,3) 什么的
select * from tb order by decode(type,1), type;
if you want to order by type=2,then you can do like this:
select * from tb order by decode(type,2), type;
用union all
先查询满足条件的,比如
select ...
where type = 1
union all
select ...
where type <> 1
sorry,
it should be like this:
type=1:select * from tb order by decode(type,1,1,2), type;
type=2:select * from tb order by decode(type,2,1,2), type;
select id,name,type from [tablename] where type=1 union
select id,name,type from [tablename] where type<>1