【问题标题】:How to retrieve hierarchial parent-child data related by multiple tables?如何检索多个表相关的分层父子数据?
【发布时间】:2015-09-29 21:17:51
【问题描述】:

我有一个网站,其中包含按类别细分的吉他课程和练习。所以你可以有类别scales。然后一节课scales lesson1,其中可能包含exercise1_1exercise1_2。对于其他类别和带有练习的课程也是如此。

课程和练习被视为节点(它是一个 Drupal 站点)。因此有一个节点表,其中包含节点 ID、节点类型(课程或练习)和标题。

这些节点的其他信息字段(课程/练习文本等)存储在每个字段的单独表中。例如,有一个 drupal_field_data_description 表,其中包含每个课程和练习的描述。

类别存储在分类法term 表中。

类别之间的关系是通过建立子父关系的分类索引表处理的(因此您可以有尺度、尺度->主要尺度等)。 对于我的问题,我只考虑一种深度的类别。

课程和练习的类别存储在drupal_field_data_field_category 表中,该表将课程和练习映射到它们所属的类别。

Exercise-Lesson 的父子关系存储在一个表 drupal_field_data_field_lesson 中,该表将练习映射到课程。

这是示例数据:

类别(drupal_taxonomy_term_data):

tid vid name
1   2   Scales
2   2   Arpeggios

课程和练习(drupal_node):

nid type        title
1   lesson      Lesson1
2   lesson      Lesson2
3   exercise    Ex1_1
4   exercise    Ex1_2
5   exercise    Ex2_1
6   exercise    Ex2_2

课程和练习的描述字段(drupal_field_data_field_description):

entity_type     bundle   entity_id  field_description_value
node            lesson   1          Lesson1Summary
node            lesson   2          Lesson2Summary
node            exercise 3          Ex1_1Summary
node            exercise 4          Ex1_2Summary
node            exercise 5          Ex2_1Summary
node            exercise 6          Ex2_2Summary

将课程和练习映射到分类法 (drupal_taxonomy_index):

nid tid
1   1
2   1
3   1
4   1
5   1
6   1

将课程和练习映射到类别 (drupal_field_data_field_category)(由于分类索引,这似乎几乎没有必要):

entity_type bundle   entity_id  field_category_tid
node        lesson   1          1
node        lesson   2          1
node        exercise 3          1
node        exercise 4          1
node        lesson   5          1
node        lesson   6          1

练习到课程的映射 (drupal_field_data_field_lesson):

entity_type     bundle      entity_id   field_lesson_target_id
node            exercise    3           1
node            exercise    4           1
node            exercise    5           2
node            exercise    6           2

所以...使用这种结构,我不知道如何构建一个返回表单结果的查询

Lesson1 Lesson1Summary
Ex1_1 Ex1_1Summary
Ex1_2 Ex1_2Summary
Lesson2 Lesson2Summary
Ex2_1 Ex2_1Summary
Ex2_2 Ex2_2Summary

请注意,第 1 课和第 2 课属于同一类别。

我需要返回这样的数据,因为对于一个类别页面(没有子类别),我需要为每节课显示一个表格,显示课程中的练习。

我可以在多个查询中完成所有这些操作,但我真的想更好地理解 SQL 连接和分组。另外,我对结果集没有死心,如上图所示。我对任何结果集持开放态度,可以让我以我描述的方式轻松显示数据(我将通过 PHP 完成)。

SQL fiddle is here

您如何建议构建这样的查询来提取以逻辑方式分组的课程及其练习(例如,我在上面显示的方式)?

似乎以这种方式获得课程和练习将相当于自连接,在其他表上有各种内部连接,但我无法将它们拼凑在一起......

【问题讨论】:

    标签: mysql join hierarchical-data


    【解决方案1】:

    好吧,经过大量阅读,我想我想通了:

    SELECT n.title, d.field_description_value, n.nid, l.field_lesson_target_id from drupal_node n
    
    JOIN drupal_field_data_field_description AS d ON d.entity_id = n.nid
    
    JOIN drupal_taxonomy_index AS t ON t.nid = n.nid
    
    LEFT JOIN drupal_field_data_field_lesson AS l ON l.entity_id = n.nid
    
    ORDER BY COALESCE(l.field_lesson_target_id, n.nid), l.field_lesson_target_id, n.nid
    

    我基于以上this post

    我的sqlfiddle is here

    这对我来说绝对是一个新领域,虽然上述工作有效,但我希望我了解 ORDER BY 和 GROUP BY 的细微差别以了解在何处/如何使用它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-13
      • 2012-06-29
      • 2017-11-13
      • 1970-01-01
      • 1970-01-01
      • 2016-01-09
      • 2018-08-03
      • 1970-01-01
      相关资源
      最近更新 更多