我有以下表格
表:约会
ID | PRICE | PAID
48 | 100 | 180
表:appointmentments_products
ID | APPOINTMENT_ID | PRODUCT_ID | TOTAL
10 | 48 | 1 | 30
11 | 48 | 9 | 30
12 | 48 | 6 | 30
我想以某种方式运行MySQL查询:
a)加入两个表,为每个appointment_id提供约会产品的“TOTAL”,如果“PAID”不等于PRICE(来自约会表)+ TOTAL(来自appointmentments_products表),则显示它。 / p>
这是我到目前为止所做的:
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
但是这个查询并没有对每个appointment_id的总和进行总结
答案 0 :(得分:0)
使用何处检查价格是否等于已付款,使用分组来查看是否已使用appointment_id进行分组。
答案 1 :(得分:0)
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;
答案 2 :(得分:0)
选择b.Appointment_Id,a.price,a.PAID,a.id,sum(b.total)AS TotalProd FROM appointmentments_products AS b内部联接约会作为On Appointment_Id = a.Id group by Appointment_Id,a.Price ,a.PAID,a.id有a.PAID!=(a.Price + sum(b.Total))