交叉表上的mysql计算

时间:2017-02-06 03:25:54

标签: mysql

我在mysql中有三个表,数据如下,我想知道如何得到以下输出。我不知道我的编码有什么问题?



 this.props.sports.map(sport => {            
        return (
//the key will not be a problem, since now it has values in it.
    <ProjectItems key={sport.name} sport={sport}  />
        );

table1规则

 this.props.sports.map(sport => {            
        return (
//the key will not be a problem, since now it has values in it.
    <ProjectItems key={sport.name} sport={sport}  />
        );

table2 rev

table3兑换

select 
    rev.memberid,
    (sum(rev.earned)/rule.revperpoint) - sum(redeem.redeempoint) as bal
from rev
left join rule on rev.rulename = rule.rulename
inner join redeem on rev.memberid = redeem.memberid
group by rev.memberid;

预期产出

rulename revperpoint
CNY       2
NY        1

1 个答案:

答案 0 :(得分:0)

根据您所需的结果,表rev中的第4行应为

25264833   600.50  CNY

在这里你不应该使用inner join,因为它只会检索记录哪个memberid是25264833,试试这个:

select 
    rev.memberid,
    (sum(rev.earned) / rule.revperpoint ) - coalesce(redeem.redeempoint, 0) as bal
from rev
left join rule on rev.rulename = rule.rulename
left join (select memberid, sum(redeem.redeempoint) as redeempoint from redeem group by memberid) redeem on rev.memberid = redeem.memberid
group by rev.memberid;

此处demo