复杂的MySQL查询谁能解决?

时间:2013-01-25 14:04:46

标签: mysql sql algorithm mysqli

我有以下表格:

Material
material_ID | material_Name
A           | Sugar
B           | Flour
C           | Egg Yolk
D           | Water
E           | Tea Powder

Product
product_ID  | product_Name | material_ID    
1           | Cake         | A              
1           | Cake         | B              
1           | Cake         | C
2           | Tea          | D
2           | Tea          | E
2           | Tea          | A              

ScaleData
record_ID   | product_ID     | material_ID  | Weight
1001        | 1              | A            | 30
1002        | 1              | B            | 11
1003        | 1              | C            | 25
1004        | 1              | A            | 31
1005        | 1              | B            | 25
1006        | 2              | D            | 15
1007        | 2              | E            | 20
1008        | 2              | E            | 21

如您所见:产品1号是蛋糕,需要材料:A,B和C才能形成完整的产品。产品2是茶,它需要材料:D,E和A,以形成完整的产品。

From the weighing table (ScaleData) we can see that there is 1 cake and 0 tea. 

record_ID: 1001, 1002, 1003 creates one complete cake.
record_ID: 1004, 1005 are remaining incomplete cake ingredients.
record_ID: 1006, 1007, 1008 are remaining incomplete tea ingredients.

Question:
A. How can i create the following result table based on data from above tables:

Result
product_ID  | product_Name  | Qty
1           | Cake          | 1
2           | Tea           | 0


B. How can i display the remaining ingredients like below?

Remaining
record_ID   | product_ID   | material_ID  | Weight
1004        | 1            | A            | 31
1005        | 1            | B            | 25
1006        | 2            | D            | 15
1007        | 2            | E            | 20
1008        | 2            | E            | 21
编辑:我目前能够通过混合使用.NET Code + SQL Query来解决这个问题。我正在寻找的是纯SQL查询解决方案。我目前的方法如下:

1. Execute: "SELECT DISTINCT product_ID FROM Product" to get a list of Product_ID.

2. Iterate through each product_ID and retrieve list of material from each product.
   Something like: "SELECT material_ID FROM Product WHERE product_ID=1"

3. Scan ScaleData table for each material and get the total row counts
   Something like:
   TotalA = "SELECT COUNT(*) FROM ScaleData WHERE product_ID=1 AND material_ID='A'
   TotalB = "SELECT COUNT(*) FROM ScaleData WHERE product_ID=1 AND material_ID='B'
   TotalC = "SELECT COUNT(*) FROM ScaleData WHERE product_ID=1 AND material_ID='C'

4. Compare variables: TotalA, TotalB, TotalC and get the lowest value.
   Lets say TotalA = 2 ; TotalB = 2 ; TotalC = 1
   LowestCount = TotalC = 1

5. Then we can tell total Qty for product 1 is 1 ( based on lowest count ).
   Remaining for Material A = 2 - 1 = 1
   Remaining for Material B = 2 - 1 = 1

这是我目前的解决方案,我知道效率不高。我更喜欢纯SQL解决方案,我希望一些SQL大师愿意帮助我..

1 个答案:

答案 0 :(得分:3)

这将是第一部分。它可能会被清理干净,但它基本上做的是每种产品中含有最少的成分,这就是你可以生产的产品数量。

SELECT product_id,product_name, min(c) Qty
FROM
(
  SELECT p.product_id, p.product_name, p.material_id, count(sd.product_id) c
  FROM product p
  LEFT JOIN scaledata sd
    ON p.product_id=sd.product_id
   AND p.material_id = sd.material_id
  GROUP BY product_id, p.product_name, p.material_id
) a
GROUP BY product_id

我为有时间写第二部分的人做了SQLfiddle to test with;)