A
A
Albert Ushakov2020-06-13 17:12:07
MySQL
Albert Ushakov, 2020-06-13 17:12:07

How to display data from one table, while using 2 more tables for the query?

Here is such an output structure, how to make such a multiple sql output?
1. Order table - List of orders .
2. Table order_product - Products added to the order.
3. Products table with all products and data.

Заказ #1 - Дата - Оплачен\Не оплачен - Сумма всего
--Товар 1 - Количество - сумма
--Товар 2 - Количество - сумма
Заказ #2 - Дата - Оплачен\Не оплачен - Сумма всего
--Товар 1 - Количество - сумма
--Товар 2 - Количество - сумма


Here are the work:
SELECT
    shop_order.id AS id_order,
    shop_order.name AS name_order,
    shop_order.adress,
    shop_order.phone,
    shop_order.status,
    order_product.product_count,
    product.name AS name_product,
    product.price,
    SUM(product.price) AS sum_total
FROM
    bot_shop_order_product order_product
INNER JOIN bot_shop_order shop_order ON
    shop_order.id = order_product.parent_id
INNER JOIN bot_shop_product product ON
    product.id = order_product.parent_id
WHERE
    order_product.magazin = 26


But now only the last order is displayed, and all the rest seem to be ignored + the amount that is summed up incorrectly

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kirill Gorelov, 2020-06-13
@fuck_ask

www.skillz.ru/dev/php/article-Obyasnenie_SQL_obedi...

F
FanatPHP, 2020-06-13
@FanatPHP

a simple join between all three tables, sort by order number
output order information in a cycle
will be repeated, so you need to remember the order, and add a condition - if the order has changed, then display information on it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question