简体   繁体   English

如何使亲子关系正常工作?

[英]How can I make parent-child relationships work?

I am building a website of engineering and construction in which I have list of products such as bridge, towers and all are linked. 我正在建立一个工程和建设网站,其中包含诸如桥梁,塔楼等产品的列表,并且所有产品都已链接。 When the user clicks on bridge then its type appears from mysql table on the same page which is in the form of image but the problem is again I want to retrieve the value ie its sub-types which are also images. 当用户单击bridge时,它的类型从mysql表以图像的形式出现在同一页上,但是问题再次是我想检索该值,即它的子类型也是图像。 And then when user clicks that image its details will be appeared. 然后,当用户单击该图像时,将显示其详细信息。 Basically its a hierarchy up to 3-4 levels. 基本上,它的层次结构可以达到3-4级。 But don't know how to make relationships between tables. 但是不知道如何在表之间建立关系。

If the items in your hierarchy can be generalized enough, you could make a single table with the columns: id, parent, information 如果您的层次结构中的项目可以被充分概括,则可以制作一个包含以下列的表格:id,parent,information

One entry could be id=Bridge, Parent=Architecture, information=picture_source 一项可能是id = Bridge,Parent = Architecture,information = picture_source

You can then make lookups like, show me every entry that has Architecture as its parent and send the list to the client. 然后,您可以进行查找,例如,向我显示以Architecture为父级的每个条目,并将列表发送给客户端。

Use InnoDB engine for mysql. 对mysql使用InnoDB引擎。 MyISAM don't support foreign keys (if you are using the default). MyISAM不支持外键(如果使用默认值)。

you can use: 您可以使用:

ALTER TABLE table_name ENGINE=InnoDB;

在此处输入图片说明

use parent -child relationships like this, 使用这样的父子关系
example: 例:

  1. when clicked on bridge - take data from child 1 table having same fk-key from main table , so your all bridge types will appear 单击网桥时-从子表1中获取具有与主表相同的fk键的数据,因此将显示所有网桥类型
  2. Do same for all your sub items 对所有子项目执行相同的操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM