我正在嘗試確定執行以下操作的最佳方法(即使我知道如何使用PHP等服務器端代碼輕鬆完成此任務,但如果可能,我只想使用postgreSQL) 。PostgreSQL將表與一對多關係
我有一個表格列出了所有公司當前的客戶訂購商品的數量以及它們應交貨的日期。例如:
Top Level Item, Quantity, Delivery Date
top_item_1, 5, 20140205
top_item_2, 10, 20140215
etc...
我有了的頂級項目(項目將由一個客戶訂購),我們需要購買,使該項目的相應的組件和購買的數量列表的另一個表零件。例如:
Top Level Item, Component, Quantity
top_item_1, component_1, 1
top_item_1, component_2, 4
top_item_1, component_3, 2
top_item_2, component_1, 2
top_item_2, component_4, 1
etc...
現在的問題是如何合併這些表,這樣我可以得到所有需要購買的物品當前客戶的訂單列表,到期時,又有多少在需要到期日。例如:
Component, Quantity, Delivery Date
component_1, 5, 20140205
component_2, 20, 20140205
component_3, 10, 20140205
component_1, 20, 20140215
component_4, 10, 20140215
etc...
對於冗長的問題,我表示歉意。以上任何幫助將不勝感激!
謝謝 〜丹尼爾
你要搜索的詞是「SQL JOIN 」。任何好的SQL教程都會爲你提供幫助。 –