SELECT all orders with more than one item and check all items status

后端 未结 2 449
暗喜
暗喜 2021-01-26 09:27

I have 2 tables:

Orders:

Id   |  Status
-----+--------
 1   |      OK
 2   | WAITING
 3   | WAITING
 4   |      OK
 5   |      OK
 6   |      OK
<         


        
相关标签:
2条回答
  • 2021-01-26 10:04
    SELECT O.Id 
    FROM Orders O 
    JOIN Order_details OD ON O.Id=OD.Order_Id
    WHERE O.Status='OK'
    GROUP BY O.Id
    HAVING count(DISTINCT OD.Id)>1
    AND SUM(CASE WHEN OD.Status NOT IN ('S1','S2') 
                 THEN 1 
                 ELSE 0 
            END) = 0
    
    0 讨论(0)
  • 2021-01-26 10:04
    select O.id
    from orders O inner join order_details OD
    on O.Id=OD.Order_Id and O.Status='OK' AND OD.Status IN ('S1','S2')
    GROUP BY O.Id
    HAVING count(DISTINCT OD.Id)>1
    
    0 讨论(0)
提交回复
热议问题