如果记录得到这样的话,如何取小于0的值?

时间:2016-06-23 03:31:59

标签: mysql sql

我的查询:

 select c.location_id as id_gudang, c.location_name as nama_gudang, a.item_code as id_barang, a.item_name as nama_barang, sum(b.item_qty) as qty
    from t_inventory as a
    join t_inventory_transaction as b on a.item_id = b.item_id
    join t_site_location as c on b.whse_id = c.location_id
    group by a.item_code

来自查询的数据

enter image description here

2 个答案:

答案 0 :(得分:0)

假设您希望qty < 0想要null个值(那些不存在于该表中的值),那么您需要使用outer join代替:

select c.location_id as id_gudang, c.location_name as nama_gudang, 
       a.item_code as id_barang, a.item_name as nama_barang, 
       sum(b.item_qty) as qty
from t_inventory as a
    left join t_inventory_transaction as b on a.item_id = b.item_id
    left join t_site_location as c on b.whse_id = c.location_id
group by a.item_code

这将返回t_inventory表中的所有记录,无论它们是否存在于其他表中。

答案 1 :(得分:0)

如果需要,可以将sql包装到子查询中,如:

WarrantyMailer.send_warranty_email(@contact, current_user.email)

或者只使用select * from ( select c.location_id as id_gudang, c.location_name as nama_gudang, a.item_code as id_barang, a.item_name as nama_barang, sum(b.item_qty) as qty from t_inventory as a join t_inventory_transaction as b on a.item_id = b.item_id join t_site_location as c on b.whse_id = c.location_id group by a.item_code) t where qty < 0

HAVING