我有四个表正在加入,并希望返回表lab_test中属于lab_test_group的所有行,并将它们显示为(粗体组...):
肌酐清除率
肌酸酐(液体)
肌酐(24小时尿)
甲状腺功能检查(1)
免费T4
甲状腺刺激素
包含相关数据的表格结构如下:
lab_test
+-------------+-----------------------------+
| lab_test_pk | lab_test |
+-------------+-----------------------------+
| 191 | Creatinine (Fluid) |
| 208 | Free T4 |
| 782 | Creatinine (24 hour Urine) |
| 1161 | Thyroid Stimulating Hormone |
+-------------+-----------------------------+
model_lab_test_lookup
+--------------------------+-------------+------------+
| model_lab_test_lookup_pk | lab_test_fk | pathway_fk |
+--------------------------+-------------+------------+
| 26 | 2 | 90 |
| 27 | 8 | 90 |
+--------------------------+-------------+------------+
lab_test_group
+-------------------+----------------------------+
| lab_test_group_pk | group_name |
+-------------------+----------------------------+
| 2 | Creatinine clearance |
| 8 | Thyroid function tests (1) |
+-------------------+----------------------------+
lab_test_group_lookup
+--------------------------+-------------------+--------------+
| lab_test_group_lookup_pk | lab_test_group_fk | lab_test_fk |
+--------------------------+-------------------+--------------+
| 6 | 2 | 191 |
| 7 | 2 | 782 |
| 41 | 8 | 208 |
| 42 | 8 | 1161 |
+--------------------------+-------------------+--------------+
我正在使用的查询是:
SELECT *
FROM lab_test_group,
lab_test_group_lookup,
model_lab_test_lookup,
lab_test
WHERE lab_test_group.lab_test_group_pk = model_lab_test_lookup.lab_test_fk
AND lab_test_group_lookup.lab_test_group_fk = lab_test_group.lab_test_group_pk
AND lab_test_group_lookup.lab_test_fk = lab_test.lab_test_pk
AND model_lab_test_lookup.pathway_fk = '$pathway_pk'
GROUP
BY lab_test_group.lab_test_group_pk
在此示例中$pathway_pk
== 90。
使用以下代码显示:
<?php
while ($row_lab_test_groups = mysql_fetch_assoc($result_lab_test_groups)){
$test_groups_array[] = $row_lab_test_groups;
echo "<tr><td colspan='5'>" . $row_lab_test_groups['group_name'] . "</td></tr>";
foreach($test_groups_array as $r){
echo "<tr><td>" . $r['lab_test'] . "</td></tr>";
}
}
?>
现在返回:
肌酐清除率 -
肌酸酐(液体)
甲状腺功能检查(1) -
肌酸酐(液体)
免费T4
问题的一部分是GROUP BY,其中只返回每个lab_test的一条记录,而不是每个lab_test_group的两个lab_tests。
问题是如何在相关的lab_test_group下显示所有实验室测试?每组只有两个。
注意:
表model_lab_test_lookup中的lab_test_fk用于保存各个lab_test行的键以及lab_test_group的键...
答案 0 :(得分:1)
你过度使用了桌面结构。实际上没有必要只使用主键和外键来“链接”表。但那列火车已经过去了,我们会对它视而不见。对您拥有的内容使用以下SQL语句:
SELECT * FROM
lab_test_group AS ABB2
JOIN lab_test_group_lookup AS ABB1 ON ABB1.lab_test_group_fk = ABB2.lab_test_group_pk
JOIN model_lab_test_lookup AS ABB3 ON ABB3.lab_test_fk = ABB2.lab_test_group_pk
JOIN lab_test AS ABB4 ON ABB4.lab_test_pk = ABB1.lab_test_fk
WHERE ABB3.pathway_fk = $pathway_pk;
在像这样的复杂查询中使用实际的JOIN运算符而不是将所有链接放在WHERE语句中是非常有利的。你将能够更好地描绘桌子之间的关节。
SQLFiddle上的工作示例here
答案 1 :(得分:0)
希望这有用:
select * from lab_test as t1
inner join lab_test_group_lookup as t2
on t1.lab_test_pk = t2.lab_test_fk
inner join lab_test_group as t3
on t3.lab_test_group_pk = t2.lab_test_group_fk
inner join model_lab_test_lookup as t4
on t4.lab_test_fk = t3.lab_test_group_pk
where t4.pathway_fk = 90
order by t2.lab_test_group_fk