# ****** sql的排序问题，进来有分******

lijun_xiao2009 2010-10-26 05:18:23
select Sh_dTGold.Tid,Sh_dTGold.Tprice,Sh_dTGold.Tquantity,Sh_dTGold.Tstate,Sh_Game.Gname,Sh_Server.Sname
from Sh_TGold,Sh_Game,Sh_Server
where Sh_dTGold.Gid = Sh_Game.Gid
and Sh_dTGold.Sid = Sh_Server.Sid
and Sh_dTGold.Gid =1 order by Sh_Server.Sname, Tquantity desc

...全文
158 点赞 收藏 32

32 条回复

jianwensheng 2010-10-28

andy_liucj 2010-10-28
``````select Sh_dTGold.Tid,Sh_dTGold.Tprice,Sh_dTGold.Tquantity,Sh_dTGold.Tstate,Sh_Game.Gname,Sh_Server.Sname
from Sh_TGold,Sh_Game,Sh_Server
where Sh_dTGold.Gid = Sh_Game.Gid
and Sh_dTGold.Sid = Sh_Server.Sid
and Sh_dTGold.Gid =1 order by Sh_Server.Sname, Tquantity desc``````

Sh_dTGold.Gid = Sh_Game.Gid
and Sh_dTGold.Sid = Sh_Server.Sid
and Sh_dTGold.Gid =1

Sh_Game.Gid=1 and
Sh_Server.Sid =1 and
Sh_dTGold.Gid =1

siegebaoniu 2010-10-28
``````/*
if object_id('tempt') is not null drop table tempt
--go
create table tempt (uid int,class int)
insert tempt
select 6, 1 union all
select 5, 2 union all
select 4, 3 union all
select 3, 4 union all
select 2, 5 union all
select 1, 6

*/

DECLARE @tmp_a TABLE (id int identity(1,1),uid int )
insert @tmp_a
select uid from tempt order by uid

DECLARE @tmp_b TABLE (id int identity(1,1),class int )
insert @tmp_b
select class from tempt order by class desc

select a.uid,b.class
from @tmp_a a
inner join @tmp_b b on a.id=b.id

/*
1	6
2	5
3	4
4	3
5	2
6	1
*/``````

fengyun142415 2010-10-28

Metrosexual 2010-10-28

siegebaoniu 2010-10-28

fpzgm 2010-10-27
[Quote=引用 22 楼 lijun_xiao2009 的回复:]

--2000,须有结构与数据来测试
uid class
----------- -----------
6 6
5 5
4 4
3 3
2 2
1 1

uid class
----------- -----------
1 6
……
[/Quote]

SQLCenter 2010-10-27

Sweet-Tang 2010-10-27
``````
DECLARE @tmp_Table TABLE (id int identity(1,1),Tid int )
``````

claro 2010-10-27

lijun_xiao2009 2010-10-27
[Quote=引用 15 楼 abuying 的回复:]

--2000,须有结构与数据来测试
uid class
----------- -----------
6 6
5 5
4 4
3 3
2 2
1 1

uid class
----------- -----------
1 6
2 5
3 4
4 3
5 2
6 ……
[/Quote]

shizheyangde 2010-10-27

Mr_Nice 2010-10-27
[Quote=引用楼主 lijun_xiao2009 的回复:]
select Sh_dTGold.Tid,Sh_dTGold.Tprice,Sh_dTGold.Tquantity,Sh_dTGold.Tstate,Sh_Game.Gname,Sh_Server.Sname
from Sh_TGold,Sh_Game,Sh_Server
where Sh_dTGold.Gid = Sh_Game.Gid
and Sh_dTGold.Sid = Sh……
[/Quote]

order by Sh_Server.Sname, Tquantity desc 地方已经是排序了。 还请lz发上数据和要求的结果看看。

``order by Sh_Server.Sname, Sh_dTGold.Tquantity desc``

seiferbb 2010-10-27

habingwang 2010-10-26
Tquantity字段若同时存在多个表中，使用此字段的地方要写明是哪个表的。

jiao3630 2010-10-26

--2000,须有结构与数据来测试
uid class
----------- -----------
6 6
5 5
4 4
3 3
2 2
1 1

uid class
----------- -----------
1 6
2 5
3 4
4 3
5 2
6 1

``````select uid,class from
(select id=row_number()over (order by uid ASC),uid from tb ) a inner join
(select id=row_number()over (order by class deSC),class from tb ) b
on a.id=b.id``````

``````select a.Sname,b.Tquantity,a.Tid,a.Tprice,a.Tstate,a.Gname
from
(select id1=row_number() over(order by Sh_Server.Sname asc)
,Sh_dTGold.Tid,Sh_dTGold.Tprice,Sh_dTGold.Tstate,Sh_Game.Gname,Sh_Server.Sname  --这里少了Sh_dTGold.Tquantity
from Sh_TGold,Sh_Game,Sh_Server
where Sh_dTGold.Gid = Sh_Game.Gid  and Sh_dTGold.Sid = Sh_Server.Sid
and Sh_dTGold.Gid =1) A
inner join
(select id2=row_number() over(order by Tquantity desc)
,Sh_dTGold.Tid,Sh_dTGold.Tprice,Sh_dTGold.Tquantity,Sh_dTGold.Tstate　--这里少了Sh_Server.Sname
from Sh_TGold,Sh_Game,Sh_Server
where Sh_dTGold.Gid = Sh_Game.Gid  and Sh_dTGold.Sid = Sh_Server.Sid
and Sh_dTGold.Gid =1) B
on A.id1=B.id2
--注a.Tid,a.Tprice,a.Tstate,a.Gname顺序可能会打乱``````

duanzhi1984 2010-10-26

SoCsy 2010-10-26

2.1w+

MS-SQL Server 疑难问题

2010-10-26 05:18