1
私は3つのテーブルを持っています。上記の表からmysqlにサブカテゴリの項目を表示するには
categorytbl
cat_id cat_name
1 Vehicles
2 Computers
subcategorytbl
sub_id sub_name sub_parent_id cat_id
1 For Rent 0 1
2 For Sale 0 1
3 Car for Rent 1 1
4 Car for Sale 2 1
5 Motorcycle for Sale 2 1
6 Boat 2 1
7 Desktop 0 1
itemtbl
item_id item_name sub_id
1 Ferrari 3
2 Ford 3
3 Isuzu 3
4 GMC 4
5 Uncategorized 2
6 Honda 5
、私はアイテム、子供と親のサブカテゴリーのリストを表示しようとしています。結果は次のようになります。ここでは
For Rent(3)
Ferrari - car for Rent
Ford - Car For Rent
Isuzu - Car For Rent
FOR SALE(3)
GMC - Car for Sale
Uncategorized - [empty]
Honda - Motorcycle for Sale
私のSQLクエリです:
SELECT COUNT(itemtbl.cat_id) AS cnt, categorytbl.cat_id AS cat_id
FROM (
categorytbl
LEFT JOIN itemtbl ON itemtbl.cat_id = categorytbl.cat_id
)
LEFT JOIN subcategorytbl ON subcategorytbl.sub_id = itemtbl.sub_id
GROUP BY cat_id
ORDER BY cnt DESC
アプリケーションレベルでこれを行う方が簡単かもしれません。どのプログラミング言語を使用していますか? – FuzzyTree
@FuzzyTree ..私はPHPを使用しています.. – smz
私はこの質問からここに精緻化しました.. http://stackoverflow.com/questions/37021147/how-to-count-the-total-items-under-the- parent-sub-category-in-mysql – smz