2013-10-25 247 views
0

我正在嘗試執行多表查詢並將結果包含到特定表中的記錄。我敢肯定,這不是很清楚,所以這裏有與相關領域的表:MySQL查詢分組結果

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') 

該查詢返回結果顯示三個waitingrooms代替兩個,大概是因爲表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'] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbspGender: ' . $wcase['gender'] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Age: ' . $wcase['age'] . '<br />Presenting Complaint: ' . $wcase['presenting_complaint'] . "</td></tr></table>"; 
     } 
    echo "</span></p></div>"; 
    } 
    ?> 

我懷疑查詢需要改變分組的情況下數據手風琴的waitingrooms表,任何想法如何,我應該這樣做:

結果使用下面的代碼打印?

編輯

感謝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'] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbspGender: ' . $wcase['gender'] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Age: ' . $wcase['age'] . '<br />Presenting Complaint: ' . $wcase['presenting_complaint'] . "</td></tr></table>"; 
     } 
    echo "</span></p></div>"; 
} 

?> 
+0

你可以做一個SQL小提琴嗎? – developerCK

+0

http://sqlfiddle.com/#!2/10e93/1 – IlludiumPu36

+0

你的查詢和php代碼是正確的,只是,你的foreach循環在while循環之下,把它放在while後面並使用嵌套的foreach.and讓我知道它是否作品 – developerCK

回答

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'] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbspGender: ' . $wcase['gender'] . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Age: ' . $wcase['age'] . '<br />Presenting Complaint: ' . $wcase['presenting_complaint'] . "</td></tr></table>"; 
     } 
    echo "</span></p></div>"; 
} 
+0

developerCK - 非常感謝,完美的作品 - 請參閱OP編輯中的工作代碼。 – IlludiumPu36