有条款问题 - 返回错误的数据

时间:2012-04-13 13:49:37

标签: mysql

我有点困惑于我一直在努力的问题,并希望得到一些帮助。我相信我应该使用HAVING子句,但无法弄清楚如何让它正常工作。与显示多个成员相反,它显示1个成员并添加所有内容。

查询应列出每个客户的总详细租赁费用大于整体平均详细租赁费用的两倍。输出应按会员编号排序。

Mem_num mem_fname mem_lname Total)detail_fee 103 Curt Knight 7.50 105 Iva Mclain 7.00 110 Lewis Rossale 9.00 111 Stacy Mann 9.00

select m.mem_num, mem_fname, mem_lname, sum(detail_fee), avg(detail_fee)
from membership as m, rental as r
left outer join detailrental as d on (r.rent_num = d.rent_num)
group by detail_fee
having sum(detail_fee);

1 个答案:

答案 0 :(得分:0)

select m.mem_num, mem_fname, mem_lname, sum(detail_fee) as sum_fee, avg(detail_fee) as avg_fee
from membership as m, rental as r
left outer join detailrental as d on (r.rent_num = d.rent_num)
group by mem_num
having sum_fee > 2 * avg_fee;

上面的查询比你的查询“有点”。问题是:

from membership as m, rental as r

这是一个交叉联接。意思是,会员中的每一行都与租赁中的每一行相关联。我猜这会给你错误的结果。这两张桌子之间没有联系吗?像

这样的东西
from membership as m 
inner join rental as r 
on m.id = r.member_id