【问题标题】:Mysql query to find the outof stockMysql查询查找缺货
【发布时间】:2023-03-19 06:53:01
【问题描述】:

我的表结构

产品表:

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 查找缺货组的查询是什么 谢谢你的帮助。

【问题讨论】:

  • 到目前为止你尝试了什么?

标签: php mysql sql node.js


【解决方案1】:

你可以使用不存在

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'

【讨论】:

    【解决方案2】:

    你可以使用聚合:

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

    【讨论】:

    • 谢谢 .. 它对我有用.. 拯救我的一天,先生..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    • 2015-06-01
    • 2015-01-15
    相关资源
    最近更新 更多