能不能用一个sql语句实现这样的select?
有三个表
card_money 卡上剩余金额 card_id 卡号[唯一] limit_money 剩余金额
card_spent 消费金额 card_id 卡号[不唯一] spent_money 消费的money
card_subscription 预约金额 card_id 卡号[不唯一] card_subscription 预约金额
一条sql语句能否取出这样数据[统计要正确,我的这个统计不正确]
select a.card_id,a.limit_money,sum(b.spent_money) as spent_money,sum(c.card_subscription) as subscription_money from card_money a inner join card_spent b on a.card_id=b.card_id left join card_subscription c on b.card_id=c.card_id group by a.card_id,b.card_id;
card_id limit_money spent_money card_subscription
00000001 1000.00 16208.80 646.20
00000002 890.20 1732.40 1096.40
00000003 1523.20 452.20
测试数据如下