【发布时间】:2017-12-28 16:42:23
【问题描述】:
我有一个master_data,其中包含所有类型的数据,例如Learning、Program 和Course。另一个表mapping 告诉您父/子关系。
这里是分组/继承关系
学习 -> 课程 -> 课程
一个学习可能有多个程序,一个程序可能有多个 培训班。此外,一个程序可以是多个学习的一部分,并且一个 课程可以是多个项目的一部分。
如何通过保留一个额外的列(例如parent)来获取数据以识别父级,从而有助于对行进行分组。
Master_data
id title description type
----------------------------------------------------------------
1 How to Present some info Learning
2 Securing Data more info Learning
3 Preparation plan more info Program
4 Protecting System info Program
5 Presentation mediums some info Program
6 know the importance some info Course
7 Notice the key concepts some info Course
8 Presenting in PPT some info Course
9 Presenting in Video format some info Course
10 Update the System some info Course
11 Chose a Strong password some info Course
映射
id learning_id program_id course_id
---------------- ----------- --------------
1 1 3 6
2 1 5 6
3 1 3 7
4 1 5 8
5 1 5 9
6 2 4 6
7 2 4 10
8 2 4 11
结果
id title description type parent
-------------------------------------------------------------------------
1 How to Present some info Learning 1 (itself)
3 Preparation plan more info Program 1
5 Presentation mediums some info Program 1
6 know the importance some info Course 3
7 Notice the key concepts some info Course 3
8 Presenting in PPT some info Course 5
9 Presenting in Video format some info Course 5
这里,课程 3,5 是学习 1 的一部分。课程 6,7 属于课程 3,课程 8,9 属于课程 5
Mysql中的上述查询
CREATE TABLE `master_data` (
`id` INT NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`description` TEXT NOT NULL,
`type` VARCHAR(45) NOT NULL,
PRIMARY KEY (`id`));
INSERT INTO `master_data` (`id`, `title`, `description`, `type`) VALUES
('1', 'How to Present', 'some info', 'Learning'),
('2', 'Securing Data', 'few more info', 'Learning'),
('3', 'Preparation plan', 'informatoin abt this', 'Program'),
('4', 'Protecting System', 'security info', 'Program'),
('5', 'Presentation mediums', 'some info', 'Program'),
('6', 'You should know the importance', 'some info', 'Course'),
('7', 'Notice the key concepts', 'some info', 'Course'),
('8', 'Presenting in PPT', 'some info', 'Course'),
('9', 'Presenting in Vedio format', 'some info', 'Course'),
('10', 'Update the System', 'some info', 'Course'),
('11', 'Chose a Strong password', 'some info', 'Course');
CREATE TABLE `mapping` (
`id` INT NOT NULL AUTO_INCREMENT,
`learning_id` INT NOT NULL,
`program_id` INT NOT NULL,
`course_id` INT NOT NULL,
PRIMARY KEY (`id`));
INSERT INTO `mapping` (`id`, `learning_id`, `program_id`, `course_id`) VALUES
('1', '1', '3', '6'),
('2', '1', '5', '6'),
('3', '1', '3', '7'),
('4', '1', '5', '8'),
('5', '1', '5', '9'),
('6', '2', '4', '6'),
('7', '2', '4', '10'),
('8', '2', '4', '11');
【问题讨论】:
-
请在问题中添加示例结果。
-
你应该把学习、程序和课程放在三个不同的表中,并在它们之间建立外键约束。
-
您能否也为您的示例数据添加预期输出
-
这跟PostgreSQL没关系吧?
-
你试过什么?你被困在哪里了?此外,您的问题还不清楚:“如何通过保留一个额外的列来获取数据,例如 parent 以识别父级,以便对行进行分组。”请使用足够的句子和短语来清楚地表达你的意思。不要把一堆单词塞进一句话里。
标签: mysql database postgresql