组或元素的嵌套
我对 Mysql 相当陌生,但我有无法解决的问题。我将给你一个例子来证明它。请注意,我知道(对于当前的示例)还有其他更简单、更有效的方法来解决它......但只是将其作为所需过程的示例。
- 首先是数据:数据是一个人的名字。
CREATE TABLE person( id INT, name VARCHAR(100) ) TYPE=innodb;
- 第二:组创建...所以这相当简单...并且可以使用带有人员外键的表“
group
”轻松完成。这些组可以是任意的,包含任意数量的人,重复...或不...(这很简单!!) - 第三:我真正的问题---我也想要有组以其他组作为元素(而不是
persons
)。这就是真正陷入困境的地方,因为我知道如何创建一组人
,一组组
(具有自引用外键)......但是我不知道如何创建一个可能有人员
和组
的组。
我很感激任何解决这个问题的建议。
非常感谢您的评论。 问候
ACombo
I'm fairly new in Mysql, but I have problem that I cannot solve. I will give you an example to demonstrate it. Please note that I know that (for current example) there are other simpler and more efficient ways to solve it... but just take it as an example of the required procedure.
- First the data: The data would be the name of a Person.
CREATE TABLE person( id INT, name VARCHAR(100) ) TYPE=innodb;
- Second: Group Creation... So this is fairly simple... and could easily done using a table '
group
' with a foreignkey to person. These groups could be arbitrary, containing any number of persons, duplicated... or not... (that is simple!!) - Third: MY REAL PROBLEM--- I also would like to have Groups that have other Groups as elements (instead of
persons
). This is where a really get stuck, because I know how to create a groups ofpersons
, a group ofgroups
(having a self-referencing foreign key)... but I don't know how to create a group that MAY HAVEpersons
ANDGroups
.
I appreciate any suggestion to solve this issue.
Thank you very much for your comments.
Regards
ACombo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会首先设置 myGroup 和 person 表。
其次,我设置了一个 myGroupGroup 表,其中包含 myGroupId、parentMyGroupId 列。这将允许您将组行与子组行相关联,即“该组中包含这些组”。如果某个组在此表中没有行,则它内没有子组。
第三,我设置了一个 personGroup 表,其中包含 personId、myGroupId 列。这将允许您将人员行与给定组相关联。如果某个组在此表中没有行,则该组中没有人员。
我对你的 SQL 做了一些调整:
1) 将
TYPE
替换为ENGINE
2) 将表名
group
替换为myGroup
> (GROUP
是保留字< /a>)祝你好运!
I'd go with firstly setting up the myGroup and person tables.
Secondly, I'd set up a myGroupGroup table with columns myGroupId, parentMyGroupId. This will allow you to relate group rows to child group rows i.e. "this group has these groups within it". If a group has no rows in this table then it has no child groups within it.
Thirdly, I'd set up a personGroup table with columns personId, myGroupId. This will allow you to relate person rows to a given group. If a group has no rows in this table then it has no persons within it.
I've tweaked your SQL a bit:
1) Replaced
TYPE
withENGINE
2) Replaced table name
group
withmyGroup
(GROUP
is a reserved word)Good luck!
选择:
Alternative: