【发布时间】:2019-10-02 08:51:30
【问题描述】:
我的以下查询工作正常:
SELECT core_condition AS name, NULL AS parent
FROM condition_theme_lookup
UNION ALL
SELECT theme_name AS name, condition_theme_lookup.core_condition AS parent
FROM theme, condition_theme_lookup
UNION ALL
SELECT strand.strand_name AS name, theme.theme_name AS parent
FROM strand
JOIN theme ON theme.theme_pk = strand.theme_fk
使用一些 PHP 的结果数组生成以下 JSON,到目前为止还不错,显示了“主题”父级的“链”子级:
{
"name": "Condition",
"children": [{
"name": "Professional",
"children": [{
"name": "Professional Behavours"
}, {
"name": "Self-Care and Self-Awareness"
}, {
"name": "Medical Ethics and Law"
}]
}, {
"name": "Leader",
"children": [{
"name": "Teamwork and Leadership"
}, {
"name": "Collaborative Practice"
}, {
"name": "Health Systems and Careers"
}]
}, {
"name": "Advocate",
"children": [{
"name": "Health Advocacy"
}, {
"name": "Aboriginal Health"
}, {
"name": "Diversity and Inequality"
}, {
"name": "Health Promotion"
}]
}, {
"name": "Clinician",
"children": [{
"name": "Scientific Knowledge"
}, {
"name": "Patient Assessment and Clinical Reasoning"
}, {
"name": "Patient Management"
}, {
"name": "Patient Perspective"
}, {
"name": "Clinical Communication"
}, {
"name": "Quality Care"
}]
}, {
"name": "Educator",
"children": [{
"name": "Life-Long Learning"
}, {
"name": "Mentoring Relationships"
}, {
"name": "Patient Education"
}, {
"name": "Teaching and Learning"
}, {
"name": "Assessment and Evaluation"
}]
}, {
"name": "Scholar",
"children": [{
"name": "Research and Biostatistics"
}, {
"name": "Evidence-Based Practice"
}, {
"name": "Information Literacy"
}]
}]
}
我现在想将相同的子集:'Year 1'、'Year 2'、'Year 3' 和 'Year 4',从表 strand.year 添加到每个 strand.strand_name 父级(例如专业行为,医学伦理和法律等)。
我尝试了以下修改后的查询:
SELECT core_condition AS name, NULL AS parent
FROM condition_theme_lookup
UNION ALL
SELECT theme_name AS name, condition_theme_lookup.core_condition AS parent
FROM theme, condition_theme_lookup
UNION ALL
SELECT strand.strand_name AS name, theme.theme_name AS parent
FROM strand, theme
UNION ALL
SELECT strand.year AS name, strand.strand_name AS parent
FROM strand
JOIN theme ON theme.theme_pk = strand.theme_fk
但正如您在下面看到的,现在的关系是不完整的;前五个节点失去了他们的孩子,只有一个,信息素养,有年孩子。
{
"name": null,
"children": [{
"name": "Professional"
}, {
"name": "Leader"
}, {
"name": "Advocate"
}, {
"name": "Clinician"
}, {
"name": "Educator"
}, {
"name": "Scholar",
"children": [{
"name": "Professional Behavours"
}, {
"name": "Self-Care and Self-Awareness"
}, {
"name": "Teamwork and Leadership"
}, {
"name": "Collaborative Practice"
}, {
"name": "Health Systems and Careers"
}, {
"name": "Health Advocacy"
}, {
"name": "Aboriginal Health"
}, {
"name": "Diversity and Inequality"
}, {
"name": "Health Promotion"
}, {
"name": "Scientific Knowledge"
}, {
"name": "Patient Assessment and Clinical Reasoning"
}, {
"name": "Patient Management"
}, {
"name": "Patient Perspective"
}, {
"name": "Clinical Communication"
}, {
"name": "Quality Care"
}, {
"name": "Life-Long Learning"
}, {
"name": "Mentoring Relationships"
}, {
"name": "Patient Education"
}, {
"name": "Teaching and Learning"
}, {
"name": "Assessment and Evaluation"
}, {
"name": "Research and Biostatistics"
}, {
"name": "Evidence-Based Practice"
}, {
"name": "Information Literacy",
"children": [{
"name": "Year 1"
}, {
"name": "Year 2"
}, {
"name": "Year 3"
}, {
"name": "Year 4"
}]
}, {
"name": "Medical Ethics and Law"
}]
}]
}
应如何更改查询以显示第一个 JSON 中的所有关系,并将四个“Year X”子代添加到每个链中?
Required JSON result up to Year children (ignore children of Year x
SQL:
原始 JSON 版本的工作 PHP/MySQL 是:
$condition = $_POST['condition'];
$query = "SELECT core_condition AS name, NULL AS parent
FROM condition_theme_lookup
UNION ALL
SELECT theme_name AS name, condition_theme_lookup.core_condition AS parent
FROM theme, condition_theme_lookup
UNION ALL
SELECT strand.strand_name AS name, theme.theme_name AS parent
FROM strand
JOIN theme ON theme.theme_pk = strand.theme_fk";
$result = $connection->query($query);
$data = array();
while ($row = $result->fetch_object()) {
$data[$row->name] = $row;
}
foreach ($data as $row) {
if ($row->name == 'Condition') {
$row->name = $condition;
}
if ($row->parent === null) {
$roots[]= $row;
} else {
$data[$row->parent]->children[] = $row;
}
unset($row->parent);
}
$json = json_encode($roots);
【问题讨论】:
-
你不应该在
year表和其他表之间有关系吗?那里没有外键,您打算如何为每个链项目获取正确的年份信息? -
在下面查看我的答案。如果你需要一个关系,然后设置它。如果没有,那么只需使用下面的解决方案。
-
我意识到年份信息只能放在链表下。如果你正在做一个 UNION,你需要相同数量的列,所以每个表都会有年份字段,这不是你想要的。
-
如果你知道它不会改变,为什么不以编程方式而不是在查询中处理它?
-
在最终的 JSON 中会有更多的子级别,所以我希望所有内容都来自数据库查询。我已经修改了 OP 以反映链表有多年。