以下3張表格,如何以最佳/更快的方式計算百萬條記錄的庫存餘額?!
作爲我的經驗,使用連接可以小於0.1秒,但並非最終結果,
使用工會可以得到最終的結果,但它需要1X秒......mysql查詢3個表,加入或聯合哪一個最好計算餘額?
stocktable stid productid qty 1 1 100 2 2 200 sellstable sellid stid qty 1 1 50 2 1 30 scraptable scrapid stid qty 1 1 10 2 1 5 3 2 100
SELECT
a.stid,
qty,
sellid,
b.qty,
scrapid,
c.qty
FROM
stocktable AS a
LEFT JOIN sellstable AS b ON a.stid = b.stid
LEFT JOIN scraptable AS c ON a.stid = c.stid
1 100 1 50 1 10 1 100 1 50 1 5 1 100 2 30 1 10 1 100 2 30 1 5 2 200 null null 3 100
此使用時加入了內部查詢,但如何總結餘額爲快...
補充信息:
the result should be 1 5 2 100
謝謝!
如何在沒有價格的情況下獲得餘額? – Matthew 2011-05-09 14:34:51
嗨,感謝您的評論。只有餘額可以! – mos 2011-05-09 15:09:22