本帖最后由 lwaif 于 2010-08-04 17:44:16 编辑

解决方案 »

  1.   


    [email protected]_monitor>create table A(id int,count int);
    Query OK, 0 rows affected (0.01 sec)[email protected]_monitor>create table B like A;
    Query OK, 0 rows affected (0.00 sec)[email protected]_monitor>insert into A values(1,50),(1,10);
    Query OK, 2 rows affected (0.00 sec)
    Records: 2  Duplicates: 0  Warnings: [email protected]_monitor>insert into B values(1,80);
    Query OK, 1 row affected (0.00 sec)[email protected]_monitor>select first.id,first.count-B.count from (select id,sum(count) as count from A group by id) as first,B where first.id=B.id;     
    +------+---------------------+
    | id   | first.count-B.count |
    +------+---------------------+
    |    1 |                 -20 | 
    +------+---------------------+
    1 row in set (0.04 sec)
      

  2.   

    select 标题,sum(数量 )
    from (
    select 标题,数量 from 表A是盘点数据
    union all
    select 标题,-数量 from 表B是库存数据
    ) t
    group by 标题