我有一个基本的SQL理解水平,因此需要帮助,并提前感谢那些回复。
我有2个表,一个表包含可在这些标题下选择的标题和选项。另一个表链接到该表的实际数据引用,以引用标题名称和选项。
我正在尝试执行SQL查询来连接这些表,然后在一个表中引用父/子ID来从另一个表中提取标题+ selected选项,但我得到的只是ID号。我已经创建了一个图像,可以解释我想要回来的结果......并且严重失败!
此处的图片将说明:
注意 - 上面,我说不是18和20,我可以让我的结果显示身份证号码......但不是父母标题和子标题中的正确信息。 (服务器支持 - 可访问的站点访问)
这是我使用SQL的地方:
SELECT custom_def_organizations.title
FROM custom_data_organizations
INNER JOIN organizations
ON custom_data_organizations.organization_id = organizations.id
INNER JOIN custom_def_organizations
ON custom_def_organizations.parent_id = custom_data_organizations.root_field_id
AND custom_def_organizations.id = custom_data_organizations.field_id
答案 0 :(得分:3)
加入父级和子级的第一个查询,没有custom_data_organization但使用隐含的层次结构:
SELECT parent.id, child.id
FROM custom_def_organizations AS parent
JOIN custom_def_organizations AS child
ON (child.parent_id = parent.id);
这将返回:
18 19
18 20
18 21
18 22
18 23
现在获取其他信息:
SELECT parent.id, child.id, CONCAT(parent.title, ' - ', child.title) AS title
FROM custom_def_organizations AS parent
JOIN custom_def_organizations AS child
ON (child.parent_id = parent.id);
这将返回:
18 19 Server Support - Yes
18 20 Server Support - Site Visits Chargeable
18 21 Server Support - Site Visits Included
18 22 ...
18 23
相同的概念,但使用custom_data_organizations驱动JOIN:
SELECT cdo.id, CONCAT(parent.title, ' - ', child.title) AS title
FROM custom_data_organizations AS cdo
JOIN custom_def_organizations AS parent
ON (cdo.root_field_id = parent.id)
JOIN custom_def_organizations AS child
ON (cdo.field_id = child.id);
这将返回:
85 Server Support - Site Visits Chargeable
...