1

我有以下 MYSQL 表

OrderId OrderItemId IsReceived
  1         21          0
  1         22          1
  2         31          1
  2         11          0
  2         21          0
  3         31          1
  3         12          1
  • 如果IsReceived== 1,则表示收到此项目。

  • 如果IsReceived== 0,则表示该项目尚未收到。

我想知道哪个订单被完全接收(All orderItemsare received),Mysql Query 是什么?

在这种情况下,它会返回一个OrderId: 3 (因为在orderId3 中,所有的订单物品都收到了)

4

3 回答 3

7

这是一个解决方案:

SELECT OrderId
FROM `IHaveFollowingMySQLTable`
GROUP BY OrderID
HAVING MIN(IsReceived) = 1
于 2013-03-08T05:16:06.587 回答
0
select distinct(tbl.OrderId) from your_table_name tbl where tbl.OrderId not in
(select distinct(tbl2.OrderId) from your_table_name tbl2 where tbl2.IsReceived=0)
于 2013-03-08T05:24:11.807 回答
0

还有一个

SELECT OrderId
  FROM  OrderItems
 GROUP BY OrderID
HAVING SUM(IsReceived) = COUNT(IsReceived)

但@Bill Karwin 的回答最为优雅。

于 2013-03-08T05:23:38.903 回答