有什麼方法可以從表連接中返回空白/空數據的不同值。最好用下面的例子來解釋一下。從mysql連接查詢中返回distinct和null記錄
表 「訂單」
order_id | order_total
1 | 10
2 | 20
3 | 50
表 「ORDER_ITEMS」
item_id | order_id | name | qty_ordered | base_price | row_total
1 | 1 | Product | 1 | 10 | 10
2 | 2 | Product | 1 | 10 | 10
3 | 2 | Product2 | 1 | 10 | 10
4 | 3 | Product | 2 | 10 | 20
5 | 3 | Product2 | 3 | 10 | 30
我想產生一個結果集,看起來像這樣。
order_id | item_id | name | qty_ordered | base_price | row_total | order_total
1 | 1 | Product | 1 | 10 | 10 | 10
2 | 2 | Product | 1 | 10 | 10 | 20
null | 3 | Product2 | 1 | 10 | 10 | null
3 | 4 | Product | 2 | 10 | 20 | 50
null | 5 | Product2 | 3 | 10 | 30 | null
我只想要order_id和order_total每個訂單一次。我認爲這是可能的某種聯接/獨立/子查詢,但唉,我已經嘗試過迄今爲止工作。
你總是讓你的SQL的樣子太美了,我需要你全面整理我的代碼。 – 2010-08-10 02:16:32
哇!太棒了。是的,如此優雅的呈現,現在看起來很簡單。有點。 非常感謝。 – Bobby 2010-08-10 03:13:01