我正在尝试进行多表查询,并将结果包含在特定表中的记录中。我确定这不是很清楚,所以这里有相关领域的表格:
waitingroom
+----------------+-------------+
| waitingroom_pk | waitingroom |
+----------------+-------------+
| 1 | PATH2201 |
+----------------+-------------+
| 2 | PATH2202 |
+----------------+-------------+
waitingroom_case_lookup
+----------------------------+----------------+---------+
| waitingroom_case_lookup_pk | waitingroom_fk | case_fk |
+----------------------------+----------------+---------+
| 1 | 1 | 1 |
+----------------------------+----------------+---------+
| 2 | 1 | 2 |
+----------------------------+----------------+---------+
| 3 | 2 | 3 |
+----------------------------+----------------+---------+
vcase
+---------+------------+------------+
| case_pk | case_title | patient_fk |
+---------+------------+------------+
| 1 | Case One | 1 |
+---------+------------+------------+
| 2 | Case Two | 2 |
+---------+------------+------------+
| 3 | Case Three | 3 |
+---------+------------+------------+
patient
+------------+------------+-----------+---------+
| patient_pk | first_name | last_name | case_fk |
+------------+------------+-----------+---------+
| 1 | John | Smith | 1 |
+------------+------------+-----------+---------+
| 2 | Will | Jones | 2 |
+------------+------------+-----------+---------+
| 3 | Mary | Ryan | 3 |
+------------+------------+-----------+---------+
我正在使用的查询是:
SELECT * FROM
waitingroom, waitingroom_case_lookup, vcase, patient
WHERE vcase.patient_fk = patient.patient_pk
AND waitingroom.waitingroom_pk = waitingroom_case_lookup.waitingroom_fk
AND vcase.case_pk = waitingroom_case_lookup.case_fk
AND vcase.active = 'y'
AND vcase.case_pk NOT IN (SELECT case_fk FROM user_case_lookup WHERE user_id = '$user_id')
此查询返回显示三个候补室而不是两个等候室的结果,可能是因为table waitingroom_case_lookup中有三个记录。我想要的是使用waitingroom_case_lookup根据候诊室对vcase数据进行分组。
至于打印结果,候补室显示为JQuery手风琴,我有一个嵌套的foreach循环,显示每个候补手风琴的案例数据。问题是,正如我之前所说,三个等候室显示,而不是两个。第二个和第三个候诊室手风琴似乎没问题,其中case_pk一个和两个显示在第二个手风琴中,case_pk三个显示在第三个手风琴中。第一支手风琴也展示了case_pk,这种手风琴不应该显示出来。
使用以下代码打印结果:
<?php
$waitingRooms = array();
while ($row_waitingrooms = mysql_fetch_assoc($result_waitingrooms)){
if($row_waitingrooms['gender'] == 'f'){
$gender = 'Female';
} else if ($row_waitingrooms['gender'] == 'm'){
$gender = 'Male';
}
$waitingRoomPK = $row_waitingrooms['waitingroom_pk'];
if (!isset($waitingRooms[$waitingRoomPK])) {
$waitingRooms[$waitingRoomPK] = array(
'waitingroom_pk' => $waitingRoomPK,
'waitingroom' => $row_waitingrooms['waitingroom'],
'cases' => array()
);
}
$waitingRooms[$waitingRoomPK]['cases'][] = array(
'case_pk' => $row_waitingrooms['case_pk'],
'patient_icon' => $row_waitingrooms['patient_icon'],
'first_name' => $row_waitingrooms['first_name'],
'last_name' => $row_waitingrooms['last_name'],
'age' => $row_waitingrooms['age'],
'gender' => $gender,
'presenting_complaint' => $row_waitingrooms['presenting_complaint']
);
echo "<h6>" . $row_waitingrooms['waitingroom'] . "</h6><div><p><span class='text'>";
foreach ($waitingRooms[$waitingRoomPK]['cases'] as $wcase){
echo "<table width='100%'><tr><td><input name='case' id='case_" . $wcase['case_pk'] . "' type='radio' value='" . $wcase['case_pk'] . "' /></td><td width='65' align='left'><div class='case_list'><img src='images/case_icons/" . $wcase['patient_icon'] . "' width='44' height='45' /></div></td><td width='350' align='left'>" . $wcase['first_name'] . ' ' . $wcase['last_name'] . '  Gender: ' . $wcase['gender'] . ' Age: ' . $wcase['age'] . '<br />Presenting Complaint: ' . $wcase['presenting_complaint'] . "</td></tr></table>";
}
echo "</span></p></div>";
}
?>
我怀疑需要更改查询以将案例数据手风琴分组到候诊室表格,任何想法我应该怎么做?
修改
感谢developerCK,我现在有以下工作代码:
<?php
while ($row_waitingrooms = mysql_fetch_assoc($result_waitingrooms)){
if($row_waitingrooms['gender'] == 'f'){
$gender = 'Female';
} else if ($row_waitingrooms['gender'] == 'm'){
$gender = 'Male';
}
$waitingRoomPK = $row_waitingrooms['waitingroom_pk'];
if (!isset($waitingRooms[$waitingRoomPK])) {
$waitingRooms[$waitingRoomPK] = array(
'waitingroom_pk' => $waitingRoomPK,
'waitingroom' => $row_waitingrooms['waitingroom'],
'cases' => array()
);
}
$waitingRooms[$waitingRoomPK]['cases'][] = array(
'case_pk' => $row_waitingrooms['case_pk'],
'patient_icon' => $row_waitingrooms['patient_icon'],
'first_name' => $row_waitingrooms['first_name'],
'last_name' => $row_waitingrooms['last_name'],
'age' => $row_waitingrooms['age'],
'gender' => $gender,
'presenting_complaint' => $row_waitingrooms['presenting_complaint']
);
}
foreach($waitingRooms as $val){
echo "<h6>" . $val['waitingroom'] . "</h6><div><p><span class='text'>";
foreach ($val['cases'] as $wcase){
echo "<table width='100%'><tr><td><input name='case' id='case_" . $wcase['case_pk'] . "' type='radio' value='" . $wcase['case_pk'] . "' /></td><td width='65' align='left'><div class='case_list'><img src='images/case_icons/" . $wcase['patient_icon'] . "' width='44' height='45' /></div></td><td width='350' align='left'>" . $wcase['first_name'] . ' ' . $wcase['last_name'] . '  Gender: ' . $wcase['gender'] . ' Age: ' . $wcase['age'] . '<br />Presenting Complaint: ' . $wcase['presenting_complaint'] . "</td></tr></table>";
}
echo "</span></p></div>";
}
?>
答案 0 :(得分:1)
在while循环后使用它,并删除你的foreach循环。 你的查询是对的。只需要改变一些PHP代码
foreach(waitingRooms as $val){
echo "<h6>" . $val['waitingroom'] . "</h6><div><p><span class='text'>";
foreach ($val['cases'] as $wcase){
echo "<table width='100%'><tr><td><input name='case' id='case_" . $wcase['case_pk'] . "' type='radio' value='" . $wcase['case_pk'] . "' /></td><td width='65' align='left'><div class='case_list'><img src='images/case_icons/" . $wcase['patient_icon'] . "' width='44' height='45' /></div></td><td width='350' align='left'>" . $wcase['first_name'] . ' ' . $wcase['last_name'] . '  Gender: ' . $wcase['gender'] . ' Age: ' . $wcase['age'] . '<br />Presenting Complaint: ' . $wcase['presenting_complaint'] . "</td></tr></table>";
}
echo "</span></p></div>";
}