How is it possible to show all orders which are not completely paid off related to a client? for e.g.
SHOW ALL order details of ‘unpaid’ ORDERS for CLIENT ‘50’
table: orders
id | order_total | client_id |
---|---|---|
1 | 15.00 | 50 |
2 | 18.50 | 50 |
3 | 40.00 | 50 |
table: order_payments
order_id | total_paid | payment_status |
---|---|---|
1 | 15.00 | paid |
2 | 3.50 | open |
2 | 12.00 | paid |
*** NOTE: Not every order has a registered payment. No registered payment should be considered "open" ***
Expected results:
order_id | order_total | client_id | outstanding |
---|---|---|---|
1 | 15.00 | 50 | 0 |
2 | 18.50 | 50 | 6.50 |
3 | 40.00 | 50 | 40.00 |
3
Answers
I tested your tables and I would do it like this:
If you have a recent enough version of MySQL (>= 8.0.14), I would recommend a lateral join:
The subquery after the
lateral
keyword searches the relevant rows in the payment table for each order, and returns the total paid (or 0 if there are no rows, or no valid payment).Lateral joins are a powerful feature in SQL (that took a long time coming into MySQL). They are efficient, flexible and expressive, and come handy in many such situations.
This should easily work.