donk68254 2014-07-25 15:58
浏览 46

MYSQL - 数量左 - 产品与序列 - 多表

How can I make a mysql that will display all a specific product with different serial number and quantity available. Take note that my client does not have a table that store quantities, what we have are:

  • received_purchase_order_detail
  • received_transfer_order_detail
  • transfer_order_detail
  • official_receipt_detail
  • ... and many more tables... these are the table we used to know how much more quantity are available.

quantity available = ( received_purchase_order_detail+received_transfer_order_detail )-( transfer_order_detail+official_receipt_detail .... )

I already have a query given by FuzzyTree but it shows a group by product quantity.

MySQL Group By Product Id BUT NOT Product that has a Serial Number

What I need now is to query the products id which has different serial numbers and show each quantity available.

Rather than

Product_ID      Product_Name    Quantity_Available
6               Apple           100
7               Blender         2
8               Toaster         3

I need

Product_ID  Product_Name    Serial_No   Quantity_Available
6           Apple           NULL        100
7           Blender         b1          0
7           Blender         b2          1
7           Blender         b3          1
8           Toaster         t1          1
8           Toaster         t2          1
8           Toaster         t3          1

-------


My test sql now Problem: i've been combining a derived table with union and made the s_product_serial group by so that they will separate. But my query is showing all of the product with serial quantity_available to 0, its like they are following the first data of the first product with a serial that is sold.., only the first blender with b1 serial is sold but the other blender should still be available..

Note I added a union query because the products table does not have serial_no column..., the serial are scattered on different tables. So what i did first is union the tables to collect the complete serial_no available. Or maybe I'm approaching this the wrong way?

Problem

Product_ID  Product_Name    Serial_No   Quantity_Available
6           Apple           NULL        100
7           Blender         b1          0
7           Blender         b2          0
7           Blender         b3          0
8           Toaster         t1          0
8           Toaster         t2          0
8           Toaster         t3          0
SELECT
                        tabletest1.i_p_id,
                        tabletest1.s_product_serial,
                        (
                            COALESCE(tabletest1.Product_Total,0) +
                            COALESCE(table1.Product_Total,0) +
                            COALESCE(table2.Product_Total,0) +
                            COALESCE(table3.Product_Total,0)
                        ) AS 'Total QTY Added',
                        (
                            COALESCE(tabletest1.Product_Total,0) +
                            COALESCE(table4.Product_Total,0) +
                            COALESCE(table5.Product_Total,0) +
                            COALESCE(table6.Product_Total,0)
                        ) AS 'Total QTY Released',
                        (
                            COALESCE(tabletest1.Product_Total,0) +
                            COALESCE(table1.Product_Total,0) +
                            COALESCE(table2.Product_Total,0) +
                            COALESCE(table3.Product_Total,0)
                        ) -
                        (
                            COALESCE(tabletest1.Product_Total,0) +
                            COALESCE(table4.Product_Total,0) +
                            COALESCE(table5.Product_Total,0) +
                            COALESCE(table6.Product_Total,0)
                        ) AS 'Current Quantity Available'
                    FROM
                        (
                                ..union query..
                        )
                        AS tabletest1
                    LEFT JOIN
                        (
                                derivedtable1..
                        ) 
                        AS table1 ON table1.i_p_id = tabletest1.i_p_id
                    LEFT JOIN 
                        (
                                derivedtable2..
                        ) 
                        AS table2 ON table2.i_p_id = tabletest1.i_p_id  
                    LEFT JOIN 
                        (
                                derivedtable3..
                        ) 
                        AS table3 ON table3.i_p_id = tabletest1.i_p_id
                    LEFT JOIN 
                        (
                                derivedtable4..
                        ) 
                        AS table4 ON table4.i_p_id = tabletest1.i_p_id
                    LEFT JOIN 
                        (
                                derivedtable5..
                        ) 
                        AS table5 ON table5.i_p_id = tabletest1.i_p_id
                    LEFT JOIN 
                        (
                                derivedtable6..
                        ) 
                        AS table6 ON table6.i_p_id = tabletest1.i_p_id
                    GROUP BY
                        i_p_id,
                        s_product_serial
  • 写回答

1条回答 默认 最新

  • duangangmo0583 2014-07-27 02:19
    关注

    i think i got it guys.

    rather than AS table(n) ON table(n).i_p_id = tabletest(n).i_p_id

    it should refer to the serial AS table(n) ON table(n).s_product_serial = tabletest(n).s_product_serial

    Now its working fine but base on how long my sql is, im wondering if this is efficient?

    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序