order by 是实现不了.只能通过机制去解决.比如先查出所有where name='金华'的记录,然后再查where name<>'金华' 然后将这两个List合并到一个List,并且优先填充where name='金华'
select * from table where name='金华' union select * from table where name<>'金华'这样OK不?
对,用union或者union all应该没有问题
select * from table where name='金华' order by id desc union all select * from table where name!='金华' 这样可以了,但是有个问题,查出来的数据是 id name 1 金华 3 金华 4 金华 2 杭州 5 北京 6 温州 想要倒叙写,不知道怎么写,order by id desc该么写 我希望查出来这样的 4 金华 3 金华 1 金华 6 温州 5 北京 2 杭州
select * from table where name='金华' union select * from table where name<>'金华' where 条件
按name='金华'的最前面,然后id倒叙 按name!='金华'的后面,然后id倒叙
第二个sql语句加个ORDER BY id DESC 应该可以解决吧.
select * from table where id='1' order by id desc union all select * from table where name!='1'
SELECT ss = CASE WHEN name='金华' THEN 0 ELSE 1 END, * FROM 表 ORDER BY ss
create table #t(id int,name nvarchar(20)) insert #t select 1,'金华' union all select 2,'杭州' union all select 3,'金华' union all select 4,'金华' union all select 5,'北京' union all select 6,'温州'select id,name,sort=case when name='金华' then 1 else 0 end from #t order by sort desc,id ascdrop table #t
select * from table where name='金华' order by id desc union select * from table where name<>'金华' order by id desc
id倒序create table #t(id int,name nvarchar(20)) insert #t select 1,'金华' union all select 2,'杭州' union all select 3,'金华' union all select 4,'金华' union all select 5,'北京' union all select 6,'温州'select id,name,sort=case when name='金华' then 1 else 0 end from #t order by sort desc,id descdrop table #t
然后将这两个List合并到一个List,并且优先填充where name='金华'
union all
select * from table where name!='金华'
这样可以了,但是有个问题,查出来的数据是
id name
1 金华
3 金华
4 金华
2 杭州
5 北京
6 温州 想要倒叙写,不知道怎么写,order by id desc该么写
我希望查出来这样的
4 金华
3 金华
1 金华
6 温州
5 北京
2 杭州
按name!='金华'的后面,然后id倒叙
union all
select * from table where name!='1'
create table #t(id int,name nvarchar(20))
insert #t select 1,'金华' union all
select 2,'杭州' union all
select 3,'金华' union all
select 4,'金华' union all
select 5,'北京' union all
select 6,'温州'select id,name,sort=case when name='金华' then 1 else 0 end from #t order by sort desc,id ascdrop table #t
insert #t select 1,'金华' union all
select 2,'杭州' union all
select 3,'金华' union all
select 4,'金华' union all
select 5,'北京' union all
select 6,'温州'select id,name,sort=case when name='金华' then 1 else 0 end from #t order by sort desc,id descdrop table #t