MYSQL查询用于选择一个表的条件的结果并在另一个表上显示它

时间:2013-05-19 08:07:58

标签: mysql

如何从一个表的前一行(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   
------------+---------

1 个答案:

答案 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