层次数据库模型的问题? [英] problem with hierarchical database model?

查看:101
本文介绍了层次数据库模型的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库设计遇到了一个问题,我正在开发一个论坛,您可以在该论坛中添加topcis,然后再添加子主题,但在该子论坛中必须显示该主题的答案以及副主题。你有一个父主题和孩子,我的问题来自id,我的意思是例如当你创建一个主题id = 1,然后你分配parent_id = 1,但你如何知道该主题的孩子,例如主题ID = 2,parent_id = 1,你可以分配给孩子id = 2,但有更好的方法吗?你如何解决这个问题呢?还有mysql我没有k

I got a problem with my db design, I am developing a forum in which you can add topcis and then subtopics inside that forum and so on but in that subforum It must show the answers of that topic, and the subtopic, so you have a parent topic and children, my problem comes with the id, I mean for example when you create a topic id=1, then you assign parent_id=1, but how do you know the children of that topic, for example topic id=2, parent_id=1, you can assign to the children id=2, but are there a better way? how would be your approach to solve this problem? also mysql I dont k

我有两个表

主题

id
name
id_creator
date
message
parent_id

id name id_creator date message parent_id

answer

id
name
message
id_creator
date

id name message id_creator date

推荐答案

我最近不得不在MySQL中执行此操作,MySQL不幸没有为处理分层数据提供任何有用的东西。

I had to do this in MySQL recently, MySQL unfortunately does not offer anything useful for processing hierarchical data.

我使用了一个名为path的额外的varchar列,其中我将存储引导到当前项的'path'。例如。一个id = 127的行是id = 120的孩子,这是id = 15的孩子将有其路径='/ 15/120/127'。

I used an extra varchar column called path where I would store the 'path' leading to current item. E.g. a row with an id=127 which is a child of id=120 which is a child of id=15 would have its path='/15/120/127'.

您需要在插入数据时自行处理。

You need to take care of this on your own when inserting data.

提取id = 15的行的直接或非直接子代的所有行将然后看起来像:

Extracting all rows that are direct or non-direct children of a row with id=15 would then look like:

SELECT * FROM table WHERE
  path LIKE '%/15/%'

我也可以看到,这是一个相当讨论的主题,在Stackoverflow上。在这个问题旁边的相关列中右边看。

I can also see that this is a rather much discussed topic here on Stackoverflow. Look on the right in Related column next to this question.

这篇关于层次数据库模型的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆