A
A
apptimeru2019-03-12 16:33:05
SQL
apptimeru, 2019-03-12 16:33:05

How to create a complex query with cross tables?

Hello everyone, please help me to create a query with a one-to-many relationship type.
Two tables are given (In order not to complicate, I will give only the necessary dates from the tables):
1. Orders in it the necessary price field
2. Payment in it the necessary sum field connection by id_orders field
The essence of the following is the main table Orders in it we store the total amount of the order, and there is a Payment table where we store all payments, one order can have several payments, respectively, there can be several rows with the same id_orders field.
You need to get the price field from Orders and the total sum of the sum fields from Payment by id_orders in one request.
Of course, it would be easy to do this with two different requests, but orders are displayed in a list and you need to see the total amount of all payments for each.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lazy @BojackHorseman, 2019-03-12
@apptimeru

SELECT
 orders.id_orders, orders.price, SUM(payment.sum)
FROM orders 
JOIN payment ON payment.id_orders = orders.id_orders 
GROUP BY orders.id_orders

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question