2012-04-21 63 views
-2

我需要顯示一些產品清單。 我有兩張桌子,一張是categories,另一張是所有products從表1中選擇類別,並從表2中選擇產品?

,我需要顯示這種結構:

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 

等等...

所以我需要從類別和產品表中的所有匹配categories_ids選擇所有categories_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 

也許我只是想錯了試圖做到這一點的一個選擇,也許我只是需要兩個選擇,第一個選擇類別名稱並通過循環選擇產品。

+1

這是基本的SQL。最好是閱讀一本書,因爲你無法通過對每個你無法處理的sql查詢提出問題來製作完整的程序。 – Aristos 2012-04-21 06:39:20

回答

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->產品列表。使用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,也沒有辦法讓這些結果在關係的方式,那麼你應該手動執行此操作,即:每行數據只輸出類別名稱,然後列出其相應的產品。

這是您應該考慮數據表示的方式,始終格式化數據以及應用程序在應用程序的表示層中的樣子,而不是數據庫端。

希望這會有所幫助。

+0

感謝MGA,我更新了我的問題。 – 2012-04-21 06:39:26

+0

@ClaesGustavsson,在您更新的查詢中,'m.userid' m是什麼的別名,並且可能是這個用戶對於每個類別只有1個產品嚐試去除'where'子句並查看結果。如果你可以包含來自你的表「類別」和「產品」的樣本數據以及你想要結果是什麼樣子的樣本,那將是非常有用的。 – 2012-04-21 06:44:04

+0

感謝MGA,再次更新。 – 2012-04-21 07:22:01