我有两张桌子
我用抽签记录所有进出存货的交易。从仓库入库或出货的每一批货物都有自己的序列号,每一笔进出交易都会被记录。
现在我想得到X仓库中仍然可用的所有序列号。基本上,我想做到这一点:
SELECT serials FROM table_a where transaction_type=In
MINUS
SELECT serials FROM table_a where transaction_type=Out我准备了两个SQL摆弄:
基本上,我想选择在这个仓库中仍然可用的所有序列化。例如,名单A-名单B。
发布于 2016-06-03 10:35:23
不确定我是否正确的理解,你应该能够使用Not IN在您的地方-条件。http://sqlfiddle.com/#!9/3a4ab/10
select IL.lot_id, ILS.serial_id
from inventory_lot_serials ILS
left join inventory_lots IL ON IL.id=ILS.inventory_lot_id
where
IL.type='In' and
IL.warehouse_location_id=500
AND ILS.serial_id NOT IN
(SELECT ILS.serial_id
from inventory_lot_serials ILS
left join inventory_lots IL ON IL.id=ILS.inventory_lot_id
where
IL.type='Out' and
IL.warehouse_location_id=500)发布于 2016-06-03 11:49:39
可能使用左外部联接来避免使用子查询:-
SELECT IL.lot_id,
ILS.serial_id
FROM inventory_lots IL
INNER JOIN inventory_lot_serials ILS ON IL.id = ILS.inventory_lot_id
LEFT OUTER JOIN inventory_lots ILO ON ILO.lot_id = IL.lot_id AND ILO.type = 'Out' AND ILO.warehouse_location_id = 500
LEFT OUTER JOIN inventory_lot_serials ILSO ON ILO.id = ILSO.inventory_lot_id AND ILS.serial_id = ILSO.serial_id
WHERE IL.type = 'In'
AND IL.warehouse_location_id = 500
AND ILSO.inventory_lot_id IS NULL这将对表中的库存执行内部连接(因为您只对有库存的记录感兴趣-在此不需要外部连接),然后是库存表的左外部连接。然后WHERE子句丢弃库存表中找到的项目。
这里的SQL小提琴:-
http://www.sqlfiddle.com/#!9/10c0a/8
https://stackoverflow.com/questions/37611954
复制相似问题