简体   繁体   English

SQL Server查询中的一对多关系

[英]One to many relationship in SQL Server query

I have two tables in SQL Server Orders and OrderDetails . 我在SQL Server OrdersOrderDetails有两个表。

  • Orders contains the OrderID and etc. Orders包含OrderID等。
  • OrderDetails contain OrderID , ItemID , Quantity and etc. OrderDetails包含OrderIDItemIDQuantity等。

These two tables have a one-to-many relationship. 这两个表具有一对多的关系。 One order can have many items. 一个订单可以有很多项目。

What is the best way to retrieve records from these 2 table, so that when I display the records in my ASP page, it will have the following tabular format? 从这两个表中检索记录的最佳方法是什么,这样当我在ASP页面中显示记录时,它将具有以下表格格式?

OrderID ItemID Quantity
    1    156     1
    2    156     2
         150     1
         188     1
    3    245     1
         344     1

The easiest is to have a query to retrieve the details from the OrderDetails table inside the main loop, but this will be very resource intensive. 最简单的方法是使用查询从主循环中的OrderDetails表中检索详细信息,但这将是非常耗费资源的。

Is there a better way to achieve this? 有没有更好的方法来实现这一目标?

The database is in SQL Server and my page is in classic ASP. 数据库在SQL Server中,我的页面在经典ASP中。

Thank you. 谢谢。

SQL: SQL:

select    o.OrderID, d.ItemID, d.Quantity
from      Orders o
          inner join OrderDetails d on o.OrderID = d.OrderID
order by  o.OrderID, d.ItemID

ASP: ASP:

store the last OrderID in a variable and whenever it's different than the last time print it, otherwise print an empty <td> 将最后一个OrderID存储在一个变量中,只要它与上次打印它不同,否则打印一个空的<td>

<% 
set lastId = -1
do while not objRS.EOF 
%>
  <tr>
<% if lastId <> objRs("OrderID") then %>
    <td><%= objRs("OrderID") %></td>
<% else %>
    <td></td>
<% end if %>
    <td><%= objRs("ItemID") %></td>
    <td><%= objRs("Quantity") %></td>
  </tr>
<% 
lastId = objRs("OrderID")
loop %>

You have 3 options here: 你有3个选择:

(a) Create a stored procedure that 'flattens' the relationship down into single rows, your presentation layer can then selectively make a choice on what to hide or display. (a)创建一个stored procedure ,将关系“展平”为单行,然后您的表示层可以有选择地选择隐藏或显示的内容。 Effectively your query should return the following data into a business object: 有效地,您的查询应将以下数据返回到业务对象:

 OrderID ItemID Quantity 1 156 1 2 156 2 2 150 1 2 188 1 3 245 1 3 344 1 

Your UI must then handle the complexity of display the parent child relationship and hiding duplicate orders. 然后,您的UI必须处理显示父子关系和隐藏重复订单的复杂性。

(b) Use an ORM like Entity Framework or nHibernate to populate a parent / child object model that reflects your table and relationship structure. (b)使用ORM(如Entity FrameworknHibernate填充反映表和关系结构的父/子对象模型。 That way the presentation layer can just write out the object model (iterate through each parent and child collection). 这样,表示层就可以写出对象模型(遍历每个父集合和子集合)。

(c) Load each orders details collection via a separate query in a loop for each order. (c)通过循环中的单独查询为每个订单加载每个订单详细信息集合。

Option (c) is the least preferred as it scales terribly because the number of database calls directly correlates to the number of orders. 选项(c)是最不受欢迎的,因为它的扩展非常严重,因为数据库调用的数量直接与订单数量相关。

Option (b) is the best approach if you are using an ORM but if not a simple solution (a) is a good method to quickly retrieve the data in one go via a stored procedure. 如果您使用ORM,则选项(b)是最佳方法,但如果不是简单解决方案(a)是通过存储过程一次快速检索数据的好方法。

Bear in mind that if you have large data-sets you may want to consider implementing paging as performance will degrade as you retrieve more data. 请记住,如果您有大型数据集,则可能需要考虑实现paging ,因为在检索更多数据时性能会降低。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM