我希望这个查询不会输出0值,但是它会输出0值。我认为and (...) > 0不会输出0值。那么,如何防止0值的输出呢?
select lot.*, sum(movement.quantity) as value
from lot
left join lot_movement as movement on lot.id = movement.lot_id
where lot.item_id = 8 and movement.storage_id = 3
and (select sum(lot_movement.quantity)
from lot_movement
where lot_movement.lot_id = lot.id
) > 0
group by lot.id;我试图添加and sum(lot_movement.quantity) \> 0,但这给出了错误invalid use of group function。
我看到了
and (select sum(lot_movement.quantity)
from lot_movement
where lot_movement.lot_id = lot.id
group by lot_movement.lot_id) > 0是多余的。不会影响结果。
发布于 2022-04-03 14:20:32
您的查询没有给出预期的结果,因为您在lot.item_id = 8 and movement.storage_id = 3子句中对where进行过滤,但是在子subselect中没有应用相同的筛选。
我不太确定您想要实现什么,但我怀疑添加一个having子句而不是子suspect解决了您的问题:
select lot.id, sum(movement.quantity) as value
from lot
left join lot_movement as movement on lot.id = movement.lot_id
where lot.item_id = 8 and movement.storage_id = 3
group by lot.id
having sum(movement.quantity) > 0https://stackoverflow.com/questions/71726561
复制相似问题