| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 716 人关注过本帖
标题:高手请指点:查询Info表中余额排名第4-6名的用户信息
只看楼主 加入收藏
xinghe6636
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2010-8-21
结帖率:0
收藏
已结贴  问题点数:20 回复次数:3 
高手请指点:查询Info表中余额排名第4-6名的用户信息
--创建表info
create table info
(
    i_Id int primary key identity(1,1) not null,
    i_Name varchar(10) not null,
    i_Address varchar(30) not null,
    i_Balance money check(i_Balance>0)
)

--创建表card
create table card
(
    c_Id int primary key identity(1,1) not null,
    i_Id int foreign key(i_Id) references info(i_Id) not null,
    c_Upda varchar(10) not null,
    c_money money not null
)

--往info表里插入数据
insert into info values('张学良','深圳',2600)
insert into info values('刘德华','广州',6500)
insert into info values('赵本山','深圳',3600)
insert into info values('李鸿章','深圳',1200)
insert into info values('张三','上海',25400)
insert into info values('李四','广州',3600)
insert into info values('王五','上海',85600)
insert into info values('赵六','广州',3600)
insert into info values('洪金宝','深圳',4800)
insert into info values('上官云清','上海',500)
insert into info values('李世民','上海',6000)

--往card表里插入数据
insert into card values(1,'In',500)
insert into card values(5,'In',4500)
insert into card values(8,'Out',3200)
insert into card values(2,'In',6300)
insert into card values(6,'Out',200)
insert into card values(4,'Out',400)
insert into card values(10,'Out',800)
go
搜索更多相关主题的帖子: 余额 Info 用户 
2010-10-23 18:21
tangyunzhong
Rank: 5Rank: 5
来 自:广西
等 级:职业侠客
威 望:4
帖 子:97
专家分:364
注 册:2010-7-28
收藏
得分:10 
回复 楼主 xinghe6636
不注意看你的问题还觉得你说得清楚了,仔细一下,就不知道你要的余额排名是最多往最少的排还是最少往最多的排啊!
还有另一个表是不是和info 表有关,你也没有说出来,不然你多放出一个表结构有什么用处,我就不明白了.
这里我只说最多往最少排名且只对info 表余额做排名的情况.

select * from (select top 3 * from (select top 6 * from info order by i_Balance desc )a order by i_Balance) b order by i_Balance

查询出来还是排第4-6名排序.
要是只要查到第4-6名且不用排序的话就用下面语句.
select top 3 * from (select top 6 * from info order by i_Balance desc )a order by i_Balance


2010-10-23 21:55
zhaoyang1008
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:30
专家分:110
注 册:2010-9-15
收藏
得分:10 
程序代码:
select id,name,adress,balance --如果最后输出的结果中允许有moneyleft字段,则这个选择就是不需要的
from
(select a.i_Id as id
        ,a.i_Name as name
        ,i_Adress as adress
        ,i_Balance as balance
        ,'moneyleft'=case b.c_Upda
                          when 'in'then a.i_Balance+b.c_money
                          when 'out' then a.i_Balance-b.c_money
from info as a inner join card as b
on a.i_id=b.i_id
order by moneyleft) as c
where id>=4 and id<=6
2010-10-26 22:32
sw4433
Rank: 1
等 级:新手上路
帖 子:65
专家分:0
注 册:2009-5-24
收藏
得分:0 
以下是引用zhaoyang1008在2010-10-26 22:32:00的发言:

select id,name,adress,balance --如果最后输出的结果中允许有moneyleft字段,则这个选择就是不需要的
from
(select a.i_Id as id
        ,a.i_Name as name
        ,i_Adress as adress
        ,i_Balance as balance
        ,'moneyleft'=case b.c_Upda
                          when 'in'then a.i_Balance+b.c_money
                          when 'out' then a.i_Balance-b.c_money
from info as a inner join card as b
on a.i_id=b.i_id
order by moneyleft) as c
where id>=4 and id<=6

我感觉楼上的用 id 排序 有问题,如果 id4,id5,id6 这几条数据已经删除了怎么办?

2010-10-31 08:36
快速回复:高手请指点:查询Info表中余额排名第4-6名的用户信息
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018328 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved