A
A
Albert Kazan2018-08-15 18:40:56
MySQL
Albert Kazan, 2018-08-15 18:40:56

How to correctly build a query from three tables?

There are three tables: Goods, Orders,
GoodsOrdersRelationshipItemsOrdersRelationship contains only IDs of Goods linked to Orders.
Products

  • id
  • Price

Orders
  • id
  • client id

CommunicationProductsOrders
  • id
  • product id
  • order id
  • quantity of such product with such id in the order

How to display orders so that there are such fields: total amount, quantity of all goods?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
daregod, 2018-08-17
@daregod

SELECT
 ol.order_id `Номер заказа`,
 c.name `Клиент`,
 count(ol.goods_id) `Всего позиций`,
 sum(ol.amount) `Кол-во товаров`,
 sum(g.cost) `Общая сумма`
FROM
 order_list ol,
 goods g,
 `order` o,
 client c
WHERE
 g.id=ol.goods_id
AND
 o.id=ol.order_id
AND
 c.id=o.client_id
GROUP BY ol.order_id;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question