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

问题描述 投票:1回答:2

我有一个master_data与所有类型的数据,如LearningProgramCourse。另一张桌子mapping告诉你父/子关系。

这是分组/层次结构

学习 - >程序 - >课程

学习可能有多个课程,一个课程可能有多个课程。此外,程序可以是多个学习的一部分,课程可以是多个程序的一部分。

如何通过保留额外的列来使数据像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的一部分。课程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');
mysql database postgresql
2个回答
3
投票

您也可以在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;

您应该有适当的约束和索引来保证数据的准确性和良好的性能。


0
投票

这个查询将解决问题,它不优雅,但完成工作

SELECT *,
(CASE
  WHEN type = 'Learning'
    THEN id
  WHEN type = 'Program'
    THEN (SELECT a.learning_id
       FROM mapping AS A
       WHERE a.program_id = id
       LIMIT 1)
  WHEN type = 'Course'
    THEN (SELECT a.program_id
       FROM mapping AS A
       WHERE a.course_id = id
       LIMIT 1)
  END
  ) AS parent
FROM master_data;

建议如果结果是一个非常大的列表,最好在正确的位置创建索引。

© www.soinside.com 2019 - 2024. All rights reserved.