【问题标题】:How to map a table row to multiple parent rows using mapping table?如何使用映射表将表行映射到多个父行?
【发布时间】:2017-12-28 16:42:23
【问题描述】:

我有一个master_data,其中包含所有类型的数据,例如LearningProgramCourse。另一个表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


【解决方案1】:

你也可以借助 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;

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

【讨论】:

    【解决方案2】:

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

    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;
    

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

    【讨论】:

    • 感谢 Gaurav garu。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 2021-08-28
    • 2010-12-12
    • 1970-01-01
    • 2020-11-23
    • 2018-12-22
    • 2023-04-09
    相关资源
    最近更新 更多