【问题标题】:Return product if there is no match in other table [duplicate]如果其他表中没有匹配,则返回产品[重复]
【发布时间】:2020-08-15 10:13:56
【问题描述】:

我有两张桌子:

Product_Table

ProductID     Name       Date  
1             ABC        2020-02-14  
2             XYZ        2020-03-05

Productbreak_Table

BreakID   Product_id      Begin         End  
34          1             2020-01-01    2020-01-30  
35          1             2020-02-01    2020-02-20  
36          2             2020-01-15    2020-01-31  
37          2             2020-02-15    2020-03-01

我的目标是只获取 Date 不在 productbreak_table 的 BeginEnd 日期之间的产品

结果应该是:

ProductID    Name
2            XYZ

【问题讨论】:

    标签: sql postgresql


    【解决方案1】:

    你会使用not exists:

    select p.*
    from products p
    where not exists (select 1
                      from productbreak pb 
                      where pb.productid = p.productid and
                            p.date between pb.begin and pb.end
                     );
    

    【讨论】:

    • 应该是 between 而不是 not between 但它有效。
    • @Stam 。 . .谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多