[英]MySQL query to group results
I'm trying to do a multi table query and contain the results to records in a specific table. 我正在尝试进行多表查询,并将结果包含到特定表中的记录中。 I'm sure that's not very clear, so here are the tables with relevant fields: 我确定这不是很清楚,所以下面是带有相关字段的表:
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 |
+------------+------------+-----------+---------+
The query that I'm using is: 我正在使用的查询是:
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')
This query returns results showing three waitingrooms instead of two, presumably because there are three records in table waitingroom_case_lookup. 该查询返回的结果显示三个等候室而不是两个,大概是因为表waitingroom_case_lookup中有三个记录。 What I want is to group the vcase data according to the waitingrooms using waitingroom_case_lookup. 我想要的是使用waitingroom_case_lookup根据候车室对vcase数据进行分组。
As for printing the results, the waitingrooms show as JQuery accordions and I have a nested foreach loop which shows the case data in each waitingroom accordion. 至于打印结果,候诊室显示为JQuery手风琴,而我有一个嵌套的foreach循环,其中显示了每个候诊室手风琴中的案例数据。 The problem is, as I said before, that three waiting rooms show, not two. 问题是,正如我之前说的,三个候诊室显示出来,而不是两个。 The second and third waitingroom accordions appear OK, with case_pk one and two showing in the second accordion and case_pk three showing in the third. 第二和第三候诊室手风琴显示为OK,第二个手风琴中显示出case_pk 1和2,而第三个中显示出case_pk 3。 The first accordion shows case_pk one as well, and this accordion shouldn't show. 第一个手风琴也显示case_pk一个,不应显示此手风琴。
The results are printed using the following code: 使用以下代码打印结果:
<?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>";
}
?>
I suspect that the query needs to be changed to group the case data accordion to the waitingrooms tables, any ideas how I should do this? 我怀疑需要更改查询以将案例数据手风琴分组到候诊室表中,有什么主意我应该怎么做?
EDIT 编辑
Thanks to developerCK I now have the following working code: 多亏了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>";
}
?>
use it after while loop, and remove your foreach loop. 在while循环之后使用它,并删除您的foreach循环。 your query is right. 您的查询是正确的。 just need to change some php code 只需要更改一些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>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.