with this query I want to select all data from two different tables, grouping the identical data of the fields of the 2 tables. The query works in part, because it groups the data of the tables, but does not group them all together. See code and image
SELECT product,
product_modified,
amount,
SUM(quantity) AS q FROM bills_stored
GROUP BY product
UNION ALL
SELECT product,
product_modified,
product_amount,
SUM(quantity) AS q FROM ordering
GROUP BY product ORDER BY q DESC
You should first take the union, and then aggregate:
SELECT
product,
SUM(quantity) AS q
FROM
(
SELECT product, quantity FROM bills_stored
UNION ALL
SELECT product, quantity FROM ordering
) t
GROUP BY product;
Note that if you also want to select the product_modified
column then rightfully it should also appear in the GROUP BY
clause.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.