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

时间:2017-12-20 07:52:39

标签: mysql database postgresql

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

这是分组/ heirarichy

学习 - >程序 - >当然

  

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

如何通过将额外的列保留为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');

2 个答案:

答案 0 :(得分: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;

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

答案 1 :(得分: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;

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