简体   繁体   中英

select sum() from multiple table with same column

I have a table for product, sales_item and a stock with following structure

Product table:

+----+-----+-------------+
| id |name |description  |
+----+-----+-------------+
| 1  |Pr1  |prod1        |
+----+-----+-------------+
| 2  |Pr2  |prod2        |
+----+-----+-------------+
| .. |...  |.....        |
+----+-----+-------------+

sales_item_details table

+-----+----------+------------+-----+
| id  | sales_id | product_id | qty |
+-----+----------+------------+-----+
| 517 |      211 |          1 | 200 |
+-----+----------+------------+-----+
| 518 |      211 |          1 | 120 |
+-----+----------+------------+-----+

and production

+----+------------+-------+
| id | product_id | qty   |
+----+------------+-------+
|  1 |          1 |    20 |
|  2 |          2 |   200 |
|  3 |          1 |    20 |
|  4 |          3 |    30 |
|  5 |          9 |    30 |
|  6 |         65 |    10 |
|  7 |         65 |    50 |
|  8 |         71 |    10 |
|  9 |         71 |    10 |
| 10 |         71 |    10 |
+----+------------+-------+

And now I am creating multiple database with same table defination and need to maintain stock

production table and product table will be maintained from single database only sales_item_details table will be different but product id will same

So how will be the query to get SUM(qty) of sales item details and view the inventory in stock

I have tried this:

SELECT
        `pr`.`id`,
        `pr`.`name`,
        sl.size,
        IFNULL(SUM(s.qty), 0) AS sales,
        IFNULL((SELECT SUM(qty) FROM production st WHERE st.product_id = `pr`.`product-id`), 0) AS stock_added
    FROM products pr
        LEFT JOIN (
            SELECT qty, product_id FROM db1.sales_item_details
            UNION ALL 
            SELECT qty, product_id FROM db2.sales_item_details 
        ) s ON pr.`id` = s.product_id
        LEFT JOIN size_list sl ON sl.id = `pr`.`product-size`
    GROUP BY s.product_id
    ORDER BY sales DESC

but getting the product which is sold

Any help will be appriciated

1st I created a view holding all sales items grouped by product id in the main database:

CREATE OR REPLACE VIEW unit_sold_all  AS
    SELECT 
        p.`product-id` AS product_id,
        (
            (SELECT IFNULL(SUM(s0.qty), 0) FROM db_1.sales_item_details s0 WHERE s0.product_id = p.`product-id`) + 
            (SELECT IFNULL(SUM(s1.qty), 0) FROM db_2.sales_item_details s1 WHERE s1.product_id = p.`product-id`) + 
            (SELECT IFNULL(SUM(s2.qty), 0) FROM db_3.sales_item_details s2 WHERE s2.product_id = p.`product-id`) + 
            (SELECT IFNULL(SUM(s3.qty), 0) FROM db_4.sales_item_details s3 WHERE s3.product_id = p.`product-id`) + 
            (SELECT IFNULL(SUM(s4.qty), 0) FROM db_5.sales_item_details s4 WHERE s4.product_id = p.`product-id`) 
        ) as total_unit_sales
FROM products p

Then in another sql, I selected the sum of the sales.

PS: I answered this question myself because this might need by another person in the future.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM