【问题标题】:how insert into product table having hierarchical categories如何插入具有分层类别的产品表
【发布时间】:2013-03-14 14:48:28
【问题描述】:

我有一个由相邻列表模型组成的表格类别:

id      name        parent_id
1   Clothing        0
2   Books           0
3   Computers       0
4   Mobiles         0
5   Movies          0
6   Music           0
7   Mens            1
8   Shirts          7
9   Formal Trousers 7
10  Jeans           7

和product_category 表:

product_id fk
category_id fk
parent_id

并且有一个产品表

product_id
category_id
parent_id
prod_name
genre
unit price
image

我如何将产品插入到具有父 ID 链接的表中,然后它又具有到类别的链接。这样我就可以从 parent_id 和类别中检索产品。 我应该将 parent_id 定义为主键吗? 帮助表示赞赏..在此先感谢..

【问题讨论】:

标签: mysql database-design


【解决方案1】:

如果我是正确的,我理解,您需要以相反的顺序检索数据。比如,parent_id + category_id -> product_id 以及 product_id -> parent_id + category_id。

我希望你做得最多,我只是建议你使用 SQL 'where' 子句从末尾检索数据。

我什至想在每个表中使用类别、父项和产品的 PK,但经过一些研究我发现我可以在不更改数据库设计的情况下检索数据。

【讨论】:

    猜你喜欢
    • 2021-09-05
    • 1970-01-01
    • 2021-01-17
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    相关资源
    最近更新 更多