-2

我需要显示一些产品列表。我有 2 张桌子,一张带有categories,一张带有所有products.

我需要显示这个结构:

list 1.
write - categories_id from categories table
write - list of products with the above categories_id from products table
end of list

next list2.
write - next categories_id from categories table
write - list of products with the above list2 categories_id from products table
end of list

等等...

因此,我需要从类别中选择所有类别 ID,并从产品表中选择所有匹配的类别 ID。

这样做的最佳方法是什么?我应该从里面选择所有categories,然后在做直到...选择所有产品...还是?

假设类别表中有 4 个类别,因此它应该显示 4 个列表,每个列表中有 10 个产品。我拥有的 sql 如下所示:

sql3 = "SELECT * from categories c inner join products p on c.categories_id = p.categories_id where p.userid ='1' group by c.categories_id order by c.categories_id;"

and the do until looks like:
do until rs.eof
rs("categoriName")

rs("productsName")

rs.movenext
loop

但这会生成 4 个列表,每个列表中只有 1 个产品?

好的,我的表格如下所示:

categories table:
categories_id, userId, categoriesName

products table:
products_id, user_id, categories_id, productsName


And the do:
<% do until rs.eof %>
Categori: <%=rs("categoriesName")%>

Product:<%=rs("productsName")%>
<% rs.movenext
loop %>

列表应如下所示:

(list 1)
Categori: Phones
Product: iPhone
Product: Samsung
Product: Nokia

(list 2)

Categori: Tvs
Product: Philips
Product: Sony
Product: Sharp

也许我只是想在一个选择中执行此操作是错误的,也许我只需要两个选择,第一个选择类别名称并循环通过该循环,然后在该循​​环内选择产品。?

4

1 回答 1

0

这是一个查询,它将获取所有类别及其对应的产品:

SELECT
    c.CategoryName, P.ProductName, p.Price, ... //What data you want to select
FROM
    CategoriesList c
INNER JOIN 
    Products p ON c.Id = p. CategoryId
ORDER BY 
    c.CategoryName, p.ProductName

这是您获取数据的最佳方式。然后,您可以在将使用此查询的应用程序中以您想要的方式输出这些结果,例如,在您的情况下,您应该将第categoryname一个打印为标题,然后将其products列表打印为子项,依此类推。

更新:例如,您如何在控制台应用程序中输出它们:

foreach( var category in categoriesList)
{
    Console.WriteLine("(List {0})", category.Id);
    Console.WriteLine("Category: {0}", category.Name);
    foreach(var product in category.Products)
    {
         Console.WriteLine("Product: {0}", product.Name); 
    }
}

这应该打印:

(list 1)
Category: Phones
Product: iPhone
Product: Samsung
Product: Nokia
(list 2)
Category: Tvs
Product: Philips
Product: Sony
Product: Sharp
...

请注意:来自数据库的数据是表格中的表格数据:

 CategoryId  | CategoryName  |  ProductName |
-------------+---------------+--------------+
      1           Phones          iPhone
      1           Phones          Samsung  
     ...

到这里,数据库的角色就结束了,因为数据库不是关于如何表示你的数据,或者你的数据看起来如何。这应该在您的应用程序端完成。您应该根据您的数据访问层(Ado.net、LINQ to SQL 等)以Cateogry->List of Products的形式获得对象关系结构列表。例如,使用 linq 你应该这样做:

var CategoriesWithProducts = Categories.Join(
Products,
c => .Id,
p => p.CategoryId,
(Category, Products) =>
   new
   {
       Category = category,
       ItsProducts = products 
   });

foreach( cateogyr in CategoriesWithProducts)
{
   Console.WriteLine("(List {0})", category.Id);
   Console.WriteLine("Category: {0}", category.Name);
   foreach(var product in category.ItsProducts)
   {
         Console.WriteLine("Product: {0}", product.Name); 
   }
}

如果您使用的是 ADO.net 或任何其他 DAL,并且无法以关系方式获取这些结果,那么您应该手动执行此操作,即:对于每行数据仅输出类别名称,然后列出其对应的产品.

这是您应该考虑数据表示的方式,始终格式化您的数据以及它在应用程序的表示层中的外观,而不是在数据库端。

希望这会有所帮助。

于 2012-04-21T06:16:08.047 回答