如何从一个表的前一行(stock_details)中减去(或添加)一行..并在另一个表中显示结果(item_details).. ??
1st table:stock_details
----------+----------+-----------
stock_name | quantity | type
------------+----------+----------
laddu | 200 | PURCHASE
Ghee | 50 | ISSUE
2nd table:item_details
----------+----------
stock_name | quantity
------------+---------
答案 0 :(得分:0)
-- INSERT INTO item_details
-- or
-- CREATE TABLE item_details
SELECT stock_name, SUM(CASE type
WHEN 'PURCHASE' THEN +quantity
WHEN 'ISSUE' THEN -quantity
END) AS quantity
FROM stock_details
GROUP BY stock_name