我使用wonton提供的語法如下
SELECT
warehouse_id,
warehouse_name,
(ifnull(ci.all_in, 0) - ifnull(co.all_out, 0)) A01_total,
(ifnull(ci.A02_in, 0) - ifnull(co.A02_out, 0)) A02_total,
(ifnull(ci.A03_in, 0) - ifnull(co.A03_out, 0)) A03_total
FROM warehouse
LEFT JOIN (
SELECT car_in, SUM(A01) all_in, SUM(A02) A02_in, SUM(A03) A03_in
FROM car_save
GROUP BY car_in
) ci ON ci.car_in = warehouse.warehouse_id
LEFT JOIN (
SELECT car_out, SUM(A01) all_out, SUM(A02) A02_out, SUM(A03) A03_out
FROM car_save
GROUP BY car_out
) co ON co.car_out = warehouse.warehouse_id
以上能列出各倉庫物品數量.
1.請問如何讓倉庫物品數量為零時不顯示這個倉庫
2.請問如何再把A01_total.A02_total.A03_total欄位再加總一次為所有倉庫物品總數
整個再做一次 Group By 吧
<pre class="c" name="code">SELECT
warehouse_id,
warehouse_name,
SUM(A01_total) A1_Total,
SUM(A02_total) A2_Total,
SUM(A03_total) A3_Total
FROM (
你的那段 SQL
) AllData
GROUP BY warehouse_id, warehouse_name
HAVING (A1_Total + A2_Total + A3_Total) > 0
kushu提到:
SELECT
warehouse_id,
warehouse_name,
(ifnull(ci.all_in, 0) - ifnull(co.all_out, 0)) A01_total,
(ifnull(ci.A02_in, 0) - ifnull(co.A02_out...(恕刪)
您好
我在PHPMYSQL測試得到
問題1:空的倉庫不顯示這個在您的語法上實現了 謝謝
請問問題2 我要怎麼繼續把這2個倉庫做出一個總數量
不好意思 新手提問 可能沒講清楚
我是要上下加總
237+136=總數
不是左右加總
謝謝
<pre class="c" name="code">SELECT SUM(A1_Total) TotalA1, SUM(A2_Total) TotalA2, SUM(A3_Total) TotalA3
FROM (
SELECT
warehouse_id,
warehouse_name,
SUM(A01_total) A1_Total,
SUM(A02_total) A2_Total,
SUM(A03_total) A3_Total
FROM (
你的那段 SQL
) A1
GROUP BY warehouse_id, warehouse_name
HAVING (A1_Total + A2_Total + A3_Total) > 0
) A2
非常感謝您的幫忙
輸出的結果正確
雖然還看不懂.會努力的去理解的