我正試圖計算股票在SQL中。我有3桌產品,購買和銷售。 產品表股票計算總和列
+----+------------------+
| id | product_name |
+----+------------------+
| 1 | apple |
|----|------------------|
| 2 |banana |
|----|------------------|
| 3 |mango |
+----+------------------+
現在顏色表
+----+------------------+
| id | color_name |
+----+------------------+
| 1 | dark |
|----|------------------|
| 2 | light |
+----|------------------+
購表人是
+-------+-------------+
| id | quantity |color
+-------+-------------+
| 1 | 15 |dark
+-------+-------------+
| 1 | 10 |light
+-------+-------------+
| 2 | 5 |dark
+-------+-------------+
| 3 | 25 |light
+-------+-------------+
,銷售表
+-------+-------------+
| id | quantity |color
+-------+-------------+
| 1 | 5 |dark
+-------+-------------+
| 1 | 5 |light
+-------+-------------+
| 2 | 5 |dark
+-------+-------------+
| 3 | 5 |light
+-------+-------------+
採購和銷售表有外鍵ID參考ences產品表的id。現在我想要計算購買和銷售以下形式的現貨供應,即區別顏色的基礎上,也
+----+------------------+-------------+
| id | product_name | quantity |color
+----+------------------+-------------+
| 1 | apple | 10 |dark
|----|------------------|-------------|
| 1 | apple | 5 |light
|----|------------------|-------------|
| 2 |banana | 0 |dark
|----|------------------|-------------|
| 3 |mango | 20 |light
+----+------------------+-------------+
如果我們有另一個表顏色會是怎樣最好查詢顏色的基礎上計算的股票太像可用數量紅色的蘋果和淺紅色的蘋果 –
我已經改變了這個問題你可以檢查它嗎 –