donglao9606 2013-11-13 03:24
浏览 122

MYSQL / PHP返回并显示来自多个表的数据作为集合

I have four tables that I'm joining and want to return all rows in table lab_test that belong to a lab_test_group and display them like (groups in bold...):


Creatinine clearance

Creatinine (Fluid)

Creatinine (24 hour Urine)

Thyroid function tests (1)

Free T4

Thyroid Stimulating Hormone


The table structures with relevant data are:

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 |
+--------------------------+-------------------+--------------+

The query that I'm using is:

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

In this example $pathway_pk == 90.

With the following code to display:

<?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>";
    }
}           
?>

Now this returns:

Creatinine clearance -

Creatinine (Fluid)

Thyroid function tests (1) -

Creatinine (Fluid)
Free T4

Part of the problem is the GROUP BY where only one record for each lab_test is returned, instead of the two lab_tests for each lab_test_group.

The question is how can I get the all the lab tests showing under the relevant lab_test_group? That is two for each group.

NOTE:

lab_test_fk in table model_lab_test_lookup is used to hold keys for individual lab_test rows as well as keys for lab_test_group...

  • 写回答

2条回答 默认 最新

  • douguabu8960 2013-11-13 03:52
    关注

    You overdid the table structures just a little bit. There's really no need to have "linking" tables with just primary and foreign keys. But that train has passed and we'll turn a blind eye to it. Use the following SQL statement on what you have:

    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;
    

    It's really advantageous to use actual JOIN operators in a complex query like this instead of putting all the links in the WHERE statement. You'll be able to picture the joints between the tables much better.

    Working example on SQLFiddle here

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?