****** 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
我想先按Sh_Server.Sname,再按Tquantity 倒序,但这样写不可以,是怎么回事

...全文
183 32 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
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
  • 打赏
  • 举报
回复
哎,sql2000.....
fpzgm 2010-10-27
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 lijun_xiao2009 的回复:]
引用 15 楼 abuying 的回复:
如果是2005的话,可以使用row_number()
--2000,须有结构与数据来测试
uid class
----------- -----------
6 6
5 5
4 4
3 3
2 2
1 1
按uid升序,class降序,想得到下列结果
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 的回复:]
如果是2005的话,可以使用row_number()
--2000,须有结构与数据来测试
uid class
----------- -----------
6 6
5 5
4 4
3 3
2 2
1 1
按uid升序,class降序,想得到下列结果
uid class
----------- -----------
1 6
2 5
3 4
4 3
5 2
6 ……
[/Quote]

你这是很好,可我的是2000数据库,不可以用row_number()函数
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发上数据和要求的结果看看。
叶子 2010-10-27
  • 打赏
  • 举报
回复
order by Sh_Server.Sname, Sh_dTGold.Tquantity desc
seiferbb 2010-10-27
  • 打赏
  • 举报
回复
你试一下先把查询建成一个视图然后再在视图里面排,应该就可以了。我之前也是这样,不知道为什么,视图就可以,查询就不行。
habingwang 2010-10-26
  • 打赏
  • 举报
回复
Tquantity字段若同时存在多个表中,使用此字段的地方要写明是哪个表的。
jiao3630 2010-10-26
  • 打赏
  • 举报
回复
abuying 2010-10-26
  • 打赏
  • 举报
回复
如果是2005的话,可以使用row_number()
--2000,须有结构与数据来测试
uid class
----------- -----------
6 6
5 5
4 4
3 3
2 2
1 1
按uid升序,class降序,想得到下列结果
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
  • 打赏
  • 举报
回复
大牛都回答过了,那我接分吧,多给点,O(∩_∩)O谢谢
加载更多回复(12)

22,302

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧