2010-06-30 107 views
0

我的情況是,我有一對多的關係,比如訂單和訂單細節,我需要獲得具有單一訂單細節的訂單。從一對多關係中獲得一對一的結果

+0

你可以發佈你的數據和你想要的結果的例子嗎?還有表結構呢?人們無法猜測你的具體情況是什麼。如果您添加了您正在使用的數據庫系統,它也將有所幫助,因爲語法可能略有不同,具體取決於它是哪一個。 – Oded 2010-06-30 07:18:00

+0

我使用ms sql,我的表格結構非常複雜,我找到了pm_2很好的答案 – Ahmed 2010-06-30 09:12:52

回答

0

如何:

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

謝謝pm_2這個很好用 – Ahmed 2010-06-30 09:11:43

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