mysql 联结 求和,MySQL内部联接并求和两列

I have the following tables

TABLE: appointments

ID | PRICE | PAID

48 | 100 | 180

TABLE: appointments_products

ID | APPOINTMENT_ID | PRODUCT_ID | TOTAL

10 | 48 | 1 | 30

11 | 48 | 9 | 30

12 | 48 | 6 | 30

I Would like to somehow run a MySQL query that will:

a) join the two tables, SUM the "TOTAL" of appointments_products for each appointment_id and if the "PAID" is not equal of the PRICE (from appointments table) + TOTAL (from appointments_products table) then to show it.

This is what I have done so far:

select a.*, b.appointment_id as AppId, b.total as ProdTotal

from appointments a

INNER JOIN appointments_products b ON a.id = b.appointment_id

But this query does not sum the total for each appointment_id

解决方案

select a.ID,a.PRICE,a.PAID,a.id as AppId,

sum(b.total) as ProdTotal

from appointments a

INNER JOIN appointments_products b ON a.id = b.appointment_id

group by a.ID,a.PRICE,a.PAID;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值