如何制作一个mysql,显示所有具有不同序列号和数量的特定产品。 请注意,我的客户没有存储数量的表, 我们拥有的是:
可用数量=(received_purchase_order_detail + received_transfer_order_detail) - (transfer_order_detail + official_receipt_detail ....)
我已经有一个由FuzzyTree提供的查询,但它显示了按产品数量分组。
MySQL Group By Product Id BUT NOT Product that has a Serial Number
我现在需要查询具有不同序列号的产品ID并显示每个可用数量。
而不是
Product_ID Product_Name Quantity_Available
6 Apple 100
7 Blender 2
8 Toaster 3
我需要
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
我的测试sql现在问题: 我已经将派生表和union组合在一起,并将s_product_serial组分开,以便它们分开。但我的查询显示所有产品的序列quantity_available为0,就像他们跟随第一个产品的第一个数据与销售的序列..,只有第一个b1序列的搅拌机出售,但另一个搅拌机应该仍然可用..
注意 我添加了一个联合查询,因为products表没有serial_no列...,序列分散在不同的表上。所以我先做的就是联合表来收集完整的serial_no。或者我可能以错误的方式接近这个?
问题
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
答案 0 :(得分:0)
我想我得到了它们。
而不是 AS表(n)ON表(n).i_p_id = tabletest(n).i_p_id
应该参考序列号 AS table(n)ON table(n).s_product_serial = tabletest(n).s_product_serial
现在它的工作正常,但根据我的sql有多长,我想知道这是否有效?