私は 2 つのカテゴリ システムを持っています。基本的には、top_category と bottom_category の 2 つのテーブルが必要です。SQL クエリを使用してすべての製品を一覧表示するサイドバーを作成しました。top_category と bottom_category のデータを 1 つの SQL クエリで取得し、bottom_category を top_category の外部キー ID でソートして、リストでそれらをループすると、正しいネストになってしまう方法はありますか?
これが私のテーブルです。
CREATE TABLE top_category (
id INT PRIMARY KEY,
NAME VARCHAR(100)
);
CREATE TABLE bottom_category (
id INT PRIMARY KEY,
NAME VARCHAR(100) ,
top_category_id INT REFERENCES top_category
);
そして、ここに私の製品テーブルがあるので、bottom_category リンクをクリックすると、bottom_category_id にリンクされた製品をリストする必要があります。
create table product (
id int primary key,
name varchar(100) ,
bottom_category_id int references bottom_category
);