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?

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?