求asp+sql 查询三张表的写法?

魔拉宝 2010-06-23 10:27:57
有三张表,如下:
表一:user (会员资料表) 字段如下:
uid usort username money
111 01 张三 200
112 01 李四 550
113 02 王五 168
114 01 陈七 860

表二:inmoney (会员充值表) 字段如下:
id uid inmoney indate
1 111 100 2010-01-10
2 112 200 2010-01-12
3 113 100 2010-01-15
4 111 200 2010-02-18
6 114 500 2010-03-12
7 113 400 2010-03-25
8 112 800 2010-04-10
9 114 600 2010-05-20


表三:outmoney (会员消费表) 字段如下:
id uid outmoney outdate
1 111 100 2010-01-26
2 113 60 2010-02-11
3 112 250 2010-02-22
4 112 200 2010-03-12
5 114 100 2010-03-20
6 113 172 2010-04-16
7 114 140 2010-05-28

根据这三张表,我想sql查询得出的结果如下:

查询条件:日期段:2010-01-01 至 2010-06-20 ,查询此时间段时的所有会员的充值金额和消费金额总计是多少?

会员类别(usort) 会员编号(uid) 会员名称(username) 累计充值金额(inmoney) 累计消费金额(outmoney) 卡余额(money)
01 111 张三 300 100 200
01 112 李四 1000 450 550
02 113 王五 500 232 168
01 114 陈七 1100 240 860


请问写SQL="select *****" 怎么写?谢谢了!!!

在线等!
...全文
89 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
lerok 2010-06-23
  • 打赏
  • 举报
回复
試試:
select usort 會員類別,uid 會員編號,username 會員名稱
,(select isnull(sum(inmoney),0) from inmoney where uid=usr.uid and indate between '2010-01-01' and '2010-06-20') 累計充值金額
,(select isnull(sum(outmoney),0) from outmoney where uid=usr.uid and indate between '2010-01-01' and '2010-06-20') 累計消費金額
,money 卡餘額
from user usr
php_wsd 2010-06-23
  • 打赏
  • 举报
回复
select usort as 会员类别,u.uid as 会员编号,username as 会员名称,(select sum(i.inmoney) from inmoney i where i.uid=u.uid) as 累计充值金额,(select sum(o.outmoney) from outmoney o where o.uid=u.uid) as 累计消费金额,(累计充值金额-累计消费金额) as 卡余额 from user u
php_wsd 2010-06-23
  • 打赏
  • 举报
回复
你使用的是什么数据库?
魔拉宝 2010-06-23
  • 打赏
  • 举报
回复
麻烦能否按我的表写出查询SQL代码,谢谢!!!!!
fumingk 2010-06-23
  • 打赏
  • 举报
回复
SQL="select bigtype from TableA"
...
if not rs.eof then
do while not rs.eof
SQL1="SELECT smalltype from TableB where bigtype='"&rs("bigtype")&"'"
...
if not inners.eof then
do while not inners.eof
response.write "<li>"&inners("smalltype")&"</li>"
inners.movenext
loop
end if
inners.close
set inners=nothing
rs.movenext
loop

end if

改下代码

魔拉宝 2010-06-23
  • 打赏
  • 举报
回复
三张表唯一关联的字段是:uid (会员编号)
魔拉宝 2010-06-23
  • 打赏
  • 举报
回复
求高手啊?????有高手在嘛?
uvvvw 2010-06-23
  • 打赏
  • 举报
回复
对了。sum时记得用下isnull。要不然数据为空的一加就为空了

uvvvw 2010-06-23
  • 打赏
  • 举报
回复
select * from [user] a

left join
(select uid,sum(inmoney) inallmoney from inmoney where indate >'2010-01-01' and indate<'2010-06-20' group by uid) b on a.uid=b.uid

left join
(
select uid,sum(outmoney) outallmoney from outmoney where outdate >'2010-01-01' and outdate<'2010-06-20' group by uid
) c on a.uid=c.uid

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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