0

I will simplify the table like this:

table order:
order_id | name
1          a
2          b
3          c

Table order_item:
item_id | fk_order_id | status
1         1             0
2         1             1
3         2             1
4         2             1
5         3             0

Ready status let say is 1, so only order_id=2 has all its items are on ready status.

How can I query select it?

4

2 回答 2

0

有几种方法可以做到这一点——这里是一种使用COUNT——gets COUNTof all并与COUNTStatus = 1进行比较:

SELECT fk_order_id
FROM (
  SELECT fk_order_id, 
     COUNT(1) totCount, 
     COUNT(CASE WHEN Status = 1 THEN 1 END) statusCount
  FROM Order_Item
  GROUP BY fk_order_id
) t
WHERE totCount = statusCount

SQL 小提琴演示

这可以合并到一个查询中,但我认为使用子查询读起来更好。

于 2013-03-22T04:13:26.850 回答
0

试试这个: SELECT order_id ,name FROM order,order_item WHERE order.order_id =order_item.fk_order_id group by order_id ,name 有 min(status)=1

于 2013-03-22T04:06:19.250 回答