日期:2014-05-19  浏览次数:20416 次

一个排序问题
select     phone   from   LIST   where   idno   =   'admin '
union   all  
select   phone   from   LIST   where   idno     =   'user '
order   by   phone

我想以phone排序   但是还想把   idno   =   'admin '   筛选出的内容放在前面,但是一加order   by   就对所有的排序了      
怎么解决呢?


------解决方案--------------------
select * from
(select idno ,phone from LIST where idno = 'admin '
union all
select idno ,phone from LIST where idno = 'user '
) a
order by case when idno= 'admin ' then 1 else 0 end desc ,phone
--多做一次子查询...
------解决方案--------------------
order by 后可加条件,如LS的

------解决方案--------------------
select phone from LIST where idno = 'admin '
union all
select phone from LIST where idno = 'user '
order by (case idno when 'admin ' then 1 else 2 end),phone
------解决方案--------------------
一楼的语法有误,修改如下:

declare @LIST table(idno varchar(8),phone varchar(20))
insert into @LIST select 'admin ', '13601000001 '
insert into @LIST select 'admin ', '13601000002 '
insert into @LIST select 'user ' , '13601000003 '
insert into @LIST select 'admin ', '13601000004 '
insert into @LIST select 'user ' , '13601000005 '
insert into @LIST select 'user ' , '13601000006 '
insert into @LIST select 'user ' , '13601000007 '
insert into @LIST select 'admin ', '13601000008 '

select
phone
from
@LIST
where
idno = 'admin ' or idno = 'user '
order by
(case idno when 'admin ' then 1 else 2 end),phone

/*
phone
--------------------
13601000001
13601000002
13601000004
13601000008
13601000003
13601000005
13601000006
13601000007
*/
------解决方案--------------------
select * from (
select phone from LIST where idno = 'admin '
union all
select phone from LIST where idno = 'user ') aa
order by (case idno when 'admin ' then 1 else 2 end),phone