Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何使用映射表将表行映射到多个父行?_Mysql_Database_Postgresql - Fatal编程技术网

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

Mysql 如何使用映射表将表行映射到多个父行?,mysql,database,postgresql,Mysql,Database,Postgresql,我有一个主数据,包含所有类型的数据,如学习、程序和课程。另一个表mapping告诉您父/子关系 这是分组/继承人 学习->程序->课程 一个学习可以有多个程序,一个程序可以有多个 课程。此外,一个程序可以是多个学习的一部分,也可以是一个 课程可以是多个课程的一部分 如何通过保留一个额外的列(如parent来标识父项)来获取数据,以便对行进行分组 主数据 id title description type -----------

我有一个
主数据
,包含所有类型的数据,如
学习
程序
课程
。另一个表
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');

这个查询将解决问题,它不是优雅的,但可以完成任务

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;
建议
如果结果是一个非常大的列表,最好在正确的位置创建索引。

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

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

请在问题中添加一个示例结果。您应该将学习,将程序和课程设置到三个不同的表中,并在它们之间建立外键约束。您还可以为示例数据添加预期输出吗?这与PostgreSQL无关,对吗?您尝试了什么?你被困在哪里?此外,您的问题还不清楚:“如何通过保留一个额外的列(如parent)来获取数据,以识别父项,从而有助于对行进行分组。”请使用足够的句子和短语清楚地说出您的意思。不要把一大堆单词塞进一句话里。谢谢Gaurav garu。