SQL按问题排序 [英] Sql order by problem

查看:130
本文介绍了SQL按问题排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

pk_id    name 
1        bhagi
2        sumit
3        raju
4        ramesh
5        mac
6        hellen
7        allen
8        krishna
9        rashmi
10       radhamadhaba


这是我的桌子.
我想按此顺序显示所有数据

前3行pk_id = 1,2,10比按名称排序的其余顺序.
表示


This is my table.
I want to show all the data in this sequence

first 3 row pk_id = 1,2,10 than rest order by name.
means

pk_id    name
1        bhagi
2        sumit
10       radhamadhaba
rest order by name like
7        allen
6        hellen
etc...


在一个查询中

在此先感谢


In one query

Thanks in advance

推荐答案

为您的例外(1,2,10)行添加一列,并在该列中设置排序顺序编号,然后按以下方式查询:
Add a column for your exceptions (1,2,10) rows and set a sort order number in that column, then query as follows:
select * from [table] where [exception] > 0 order by [exceptions]
union 
select * from [table] where [exception] = 0 order by [name]






EDIT :

select * from [table] where pk_id in (1,2,10)
union
select * from [table] where pk_id not in (1,2,10) order by [name]


(select * from [table] where pk_id in (1,2,10)order by pk_id limit 1000)
union 
(select * from [table] order by name limit 1000)


(select * from [table] where pk_id in (1,2,10))
union 
(select * from (SELECT * FROM [table] where pk_id not in (1,2,10) order by [name]) as somename )



只需尝试使用此代码即可.

甚至不需要(1,2,10)中的pk_id也不需要,因为UNION会删除所有重复的条目.



Just try this code it should work.

Even pk_id not in (1,2,10) is not required because UNION will remove all duplicate entries.


这篇关于SQL按问题排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆