一张表包括出库和入库状态:
货品名字 出入库 数量
A        出库   100
A        入库    200
A        出库    null
求商品A的库存总数 入库减去出库
我当时是在MYSQL环境下写的
不知道怎么处理空值望各位大虾 帮助

解决方案 »

  1.   


    select SUM(IF(出入库='入库',数量,-数量))
    from 一张表
    where 货品名字='A'
      

  2.   

    处理null,可以用IFNULL函数IFNULL(数量,0)
      

  3.   

    我在数据库中写下如下代码,其中没有goods_id =4 的货品
    select sum(ifnull(repertory_number,0)) from repertory where goods_id =4
    但是写出来仍然是
    null
      

  4.   

    这是测试结果,建议你提供实际的测试用例,以及期望的正确结果。
    mysql> select * from t_bllizard;
    +-------+------+------+
    | marno | mflg | qty  |
    +-------+------+------+
    | A     | O    |  100 |
    | A     | I    |  200 |
    | A     | O    | NULL |
    +-------+------+------+
    3 rows in set (0.00 sec)mysql> select sum(if(mflg='I',qty,-qty))
        -> from t_bllizard
        -> where marno='A';
    +----------------------------+
    | sum(if(mflg='I',qty,-qty)) |
    +----------------------------+
    |                        100 |
    +----------------------------+
    1 row in set (0.00 sec)mysql>
      

  5.   

    假如:
    mysql> select * from t_bllizard;
    +-------+------+------+
    | marno | mflg | qty  |
    +-------+------+------+
    | A     | I    |  200 |只有入库记录 没有出库,
    我要求出库存量(入库减去出库)为多少怎么办?谢谢
      

  6.   

    一样的SQL语句。mysql> delete from t_bllizard;
    Query OK, 3 rows affected (0.08 sec)mysql> insert into t_bllizard values
        -> ('A','I',200);
    Query OK, 1 row affected (0.06 sec)mysql> select * from t_bllizard;
    +-------+------+------+
    | marno | mflg | qty  |
    +-------+------+------+
    | A     | I    |  200 |
    +-------+------+------+
    1 row in set (0.00 sec)mysql> select sum(if(mflg='I',qty,-qty))
        -> from t_bllizard
        -> where marno='A';
    +----------------------------+
    | sum(if(mflg='I',qty,-qty)) |
    +----------------------------+
    |                        200 |
    +----------------------------+
    1 row in set (0.00 sec)mysql>