1
說我有一個包含以下內容的數據庫:如何正確查詢數據庫中包含列表結果
orders(id, total, address_id)
line_items(id, product_id, order_id, quantity, price)
products(id, name, price)
addresses(id, address)
我需要提供以下包含報表:
Order #123
Line Items
3 x Product 1 @ $3 = $9
2 x Product 2 @ 1 = $2
Total: $11
Shipping: 123 Main Street
Order #124
Line Items
1 x Product 1 @ $3 = $3
1 x Product 2 @ 1 = $1
Total: $4
Shipping: 456 Jones Ave
所以,需要顯示訂單列表,每個訂單,ID,訂單項列表,訂單總額和送貨地址。
什麼是最好的方式做到這一點,而無需查詢每個訂單來獲取訂單項和貨運信息?
我應該有一個包含以下內容的觀點:在應用程序代碼
order_id, line_item_quantity, line_item_price, product_name, line_item_total, shipping_address
然後按順序編號?這是我能想到的最好的,但似乎馬虎(當然,在現實世界中,每張桌子上會有更多的信息和更多的表格)。
正確,所以基本上就是我所說的 - 有一個視圖,它列出了一個平面表中的所有值,然後使用知道如何對其進行分組的應用程序代碼。 – 2012-01-07 18:06:19