mysql查询查找缺货

wbgh16ku  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(311)

我的表结构
产品表:

id model_id status
1  3        SELL_IN
2  3        SELL_OUT
3  1        SELL_OUT
4  1        SELL_OUT
5  3        SELL_IN
6  2        SELL_IN
7  2        SELL_OUT
8  1        SELL_OUT

条件应为:
如果 model_id 那已经 SELL_OUT 只有和没有 SELL_IN 表中的状态将缺货,我想得到所有这些型号。
如果产品售罄(如图所示),产品状态将发生变化 SELL_OUT )将查询出什么样的库存按型号id分组谢谢您的帮助。

1mrurvl1

1mrurvl11#

您可以使用“不存在”

select model_id
from table a
where not exists( select 1 from table b where a.model_id=b.model_id
                                         and status='SELL_IN')
and a.status='SELL_OUT'
vptzau2j

vptzau2j2#

可以使用聚合:

select model_id
from t
group by model_id
having min(status) = max(status) and min(status) = 'SELL_OUT';

相关问题