1 wu
2 peng
3 jiang
4 wu
6 jiang
7 peng
8 shi
9 wu
10 peng
结果是(先按ID排序,还要把相同的name放在一起)
1 wu
4 wu
9 wu
2 peng
7 peng
10 peng
3 jiang
6 jiang
8 shi
2 peng
3 jiang
4 wu
6 jiang
7 peng
8 shi
9 wu
10 peng
结果是(先按ID排序,还要把相同的name放在一起)
1 wu
4 wu
9 wu
2 peng
7 peng
10 peng
3 jiang
6 jiang
8 shi
insert @t select 1 ,'wu'
union all select 2 ,'peng'
union all select 3 ,'jiang'
union all select 4 ,'wu'
union all select 6 ,'jiang'
union all select 7 ,'peng'
union all select 8 ,'shi'
union all select 9 ,'wu'
union all select 10,'peng'select * from @t a order by (select min(id) from @t b where b.name=a.name),name,id/*
id name
----------- ----------
1 wu
4 wu
9 wu
2 peng
7 peng
10 peng
3 jiang
6 jiang
8 shi
*/
order by xh,name
order by id
insert @t select 1 ,'wu'
union all select 2 ,'peng'
union all select 3 ,'jiang'
union all select 4 ,'wu'
union all select 6 ,'jiang'
union all select 7 ,'peng'
union all select 8 ,'shi'
union all select 9 ,'wu'
union all select 10,'peng'
这个是什么意思呢?是自己写的还是?
1 wu
4 wu
9 wu
8 shi
2 peng
7 peng
10 peng
3 jiang
6 jiang
declare @t table (id int,name varchar(10))
insert @t select 1 ,'wu'
union all select 2 ,'peng'
union all select 3 ,'jiang'
union all select 4 ,'wu'
union all select 6 ,'jiang'
union all select 7 ,'peng'
union all select 8 ,'shi'
union all select 9 ,'wu'
union all select 10,'peng'
这个是什么意思呢?是自己写的还是?
-----------------------------------------
这是测试数据.
至于楼上的几位朋友写的 order by name desc,id 纯属数据碰巧.
如果我第一条数据name='a'呢, desc还不排到最后去了.
order by id
from tb
order by name,id
完全正确。
select id ,name from t order by substring(name,1,1) desc
或
select id ,name from t order by left(name,1,1) desc
说明:
取出NAME字段的第一个字符进行排序即可
系统会自动按照字母的ASC码排序,就是你要的那种效果
可以是A,B,C....
也可以是Z,Y,X...
========================