我需要顯示一些產品清單。 我有兩張桌子,一張是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
也許我只是想錯了試圖做到這一點的一個選擇,也許我只是需要兩個選擇,第一個選擇類別名稱並通過循環選擇產品。
這是基本的SQL。最好是閱讀一本書,因爲你無法通過對每個你無法處理的sql查詢提出問題來製作完整的程序。 – Aristos 2012-04-21 06:39:20