从一对多的关系中获得一对一的结果

时间:2010-06-30 07:11:47

标签: sql relation

我的情况是我有一对多的关系,比如订单和订单详细信息,我需要获得具有单个订单详细信息的订单。

2 个答案:

答案 0 :(得分:0)

怎么样:

select * 
from order
where order_number in
    (select order_number
     from order_details
     group by order_number
     having count(*) = 1)

答案 1 :(得分:0)

SELECT O1.order_number
  FROM Orders AS O1
 WHERE 1 = (
            SELECT COUNT(*)
              FROM OrderDetails AS D1
             WHERE O1.order_number = D1.order_number
           );