我有一个 2 类别系统,基本上我想做的是我有 2 个表,top_category 和 bottom_category,我创建了我的侧边栏,它将使用 sql 查询列出所有产品。有没有办法我可以在一个 sql 查询中提取 top_category 和 bottom_category 数据,并让 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
);
这是我的产品表,所以当我单击底部类别链接时,我希望它列出链接到底部类别 ID 的产品:
create table product (
id int primary key,
name varchar(100) ,
bottom_category_id int references bottom_category
);