MySQL Inner join and sum two columns

后端 未结 3 1262
时光取名叫无心
时光取名叫无心 2021-01-29 10:22

I have the following tables

TABLE: appointments

ID | PRICE | PAID
48 |  100  | 180

TABLE: appointments_product

相关标签:
3条回答
  • 2021-01-29 10:52

    Use where to check if price is equal to paid and the use group by to group with appointment_id.

    0 讨论(0)
  • 2021-01-29 10:54
    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;
    
    0 讨论(0)
  • 2021-01-29 10:57

    select b.Appointment_Id, a.price, a.PAID, a.id, sum(b.total) AS TotalProd FROM appointments_products AS b inner join appointments as a On Appointment_Id = a.Id group by Appointment_Id, a.Price , a.PAID , a.id HAVING a.PAID != (a.Price + sum(b.Total))

    0 讨论(0)
提交回复
热议问题