mysql - 如何使用映射表将表行映射到多个父行?

标签 mysql database postgresql

我有一个master_data,其中包含所有类型的数据,例如LearningProgramCourse。另一个表 mapping 告诉您父/子关系。

这里是分组/继承

学习 -> 程序 -> 类(class)

A Learning may have multiple Programs, a Program may have multiple Courses. Also, a Program can be part of multiple Learnings and a Course can be part of multiple Programs.

如何通过保留一个额外的列来引入数据,例如 parent 来标识父级,以便它有助​​于对行进行分组。

主数据

    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 的一部分。类(class) 6,7 属于程序 3,类(class) 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');

最佳答案

你也可以在 UNION 子句的帮助下完成:

select id,title,description,type,id from master_data where type='Learning'
UNION
select program_id,title,description,type,learning_id from master_data md, mapping m where md.id=m.program_id
UNION
select course_id,title,description,type,program_id from master_data md, mapping m where md.id=m.course_id;

为了数据的准确性和良好的性能,您应该有适当的约束和索引。

关于mysql - 如何使用映射表将表行映射到多个父行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47901177/

相关文章:

php - 显示表数据,如果

mysql - Perl TLS1.2 到 MySql Db

php - 获取数据以便在表单上进行编辑

database - 数据库中没有更多的假脱机空间

ruby-on-rails - 显示错误的引用列不为空 - rails

postgresql - 具有数据库中多个表的 Tableau Extract API

php - 如何将SQL中的第一列更改为文本格式到XLS?

python - mysql为一个人存储多个别名

mysql - 名称-值对模型 对于仅配置数据

ruby-on-rails - 如何从我的应用程序数据库中正确排序日期