2012-05-03 88 views
0

我有一個包含事件的表,要列出這些事件,我循環瀏覽事件表,檢查事件類型並使用它的eventId查找它的特定表中的值。將mysql查詢與子查詢結果合併到一個PHP數組中

目前,這使用一個查詢來獲取20個事件,然後最多3個查詢來獲取這些事件的數據。我目前使用程序編碼,但需要對其進行更改,以便在最後它僅以數組形式返回數據。

這裏是什麼,我需要實現Pseduo代碼示例:

while(eventQuery): 

    if commentQueryResult; 
     $array .= commentQueryResult; 

    if forumPostQueryResult; 
     $array .= forumPostQueryResult; 

    if uploadItemQueryResult; 
     $array .= uploadItemQueryResult; 

endwhile; 

return $array; // Combined returned results as one array 

我隨後將能夠通過它來訪問數據,只是foreach循環。

我只是不確定將多個結果集合到一個數組中的最佳方式?

或者你可以嘗試,並結合成一個查詢...

$eventResult = mysql_query(
    'SELECT userevent.event, userevent.eventId, userevent.friendId 
    FROM userevent 
    WHERE userevent.userId = 1 OR userevent.friendId = 1 
    ORDER BY userevent.id DESC 
    LIMIT 20' 
); 

while ($eventRow = mysql_fetch_row($eventResult)){ 

    if($eventRow[0] == 1){ 

     $result = mysql_fetch_array(mysql_query(" 
      SELECT forumRooms.id, forumRooms.title                      
      FROM forumPosts             
      INNER JOIN forumRooms ON forumPosts.`forumTopic` = forumRooms.`id`              
      WHERE forumPosts.id = '$eventRow[1]'")); 
    } 
    elseif($eventRow[0] == 2){ 

     $result = mysql_fetch_array(mysql_query(" 
      SELECT game.id, game.uriTitle, game.title               
      FROM usergamecomment 
      INNER JOIN game ON usergamecomment.`gameId` = game.id 
      WHERE usergamecomment.id = $eventRow[1]")); 
    } 
    elseif($eventRow[0] == 4){ 

     $result = mysql_fetch_array(mysql_query(" 
      SELECT usercomment.comment, UNIX_TIMESTAMP(usercomment.TIME), `user`.id, `user`.username, `user`.activate 
      FROM usercomment 
      INNER JOIN `user` ON usercomment.`userId` = `user`.id 
      WHERE usercomment.id = $eventRow[1] 
      AND `user`.activate = 1")); 
    } 
    elseif($eventRow[0] == 5){ 

     $result = mysql_fetch_array(mysql_query(" 
      SELECT game.id, game.title, game.uriTitle 
      FROM game 
      WHERE game.id = $eventRow[1]")); 
    } 

// Combined Results as array 
} 

我將所有的這些對PDO的過程是,這是工作的最佳出路後的下一步儘量減少這一點。

+0

通過連接表是不可能用單個查詢來查詢事件列表和事件類型表?沿着以下幾行的東西:'SELECT * FROM event e INNER JOIN eventType et ON e.eventID = et.eventID' 雖然我也完全有可能誤解了你的問題。 – andrewsi

+0

沒有連接會太複雜,它通過使用php的第一個查詢循環來生成從其他結果返回的數據數組。 – Dan

+1

太複雜?我會說試試我們。至少,我喜歡腦筋急轉彎。 ;) – Paul

回答

1

接受挑戰。 ;)

因爲你實際上只對while循環內的結果感興趣,所以你可以試試這個單個查詢。由於左連接它可能不會更快,幾乎取決於您的數據庫。最後的$result包含所有元素及其各自的字段。

$result = array(); 
$q = 'SELECT userevent.event AS userevent_event, 
     userevent.eventId AS userevent_eventId, 
     userevent.friendId AS userevent_friendId, 
     forumRooms.id AS forumRooms_id, 
     forumRooms.title AS forumRooms_title, 
     game.id AS game_id, 
     game.uriTitle AS game_uriTitle, 
     game.title AS game_title, 
     usercomment.comment AS usercomment_comment, 
     UNIX_TIMESTAMP(usercomment.TIME) AS usercomment_time, 
     user.id AS user_id, 
     user.username AS user_username, 
     user.activate AS user_activate, 
     g2.id AS game2_id, 
     g2.uriTitle AS game2_uriTitle, 
     g2.title AS game2_title 


    FROM userevent 
    LEFT JOIN forumPosts ON forumPosts.id = userevent.eventId 
    LEFT JOIN forumRooms ON forumPosts.forumTopic = forumRooms.id 
    LEFT JOIN usergamecomment ON usergamecomment.id = userevent.eventId 
    LEFT JOIN game ON usergamecomment.gameId = game.id 
    LEFT JOIN usercomment ON usercomment.id = userevent.eventId 
    LEFT JOIN user ON usercomment.userId = user.id 
    LEFT JOIN game g2 ON userevent.eventId = g2.id 
    WHERE (userevent.userId = 1 OR userevent.friendId = 1) 
     AND userevent.eventId >= (SELECT userevent.eventId 
       WHERE userevent.userId = 1 OR userevent.friendId = 1 
       ORDER BY userevent.id DESC LIMIT 1,20);'; 

$r = mysql_query($q); 

while ($o = mysql_fetch_row($r)) { 
    switch($o['userevent_event']) { 
    case 1: 
     $result[] = array(
    'id' => $o['forumsRooms_id'], 
    'title' => $o['forumsRooms_title'], 
    ); 
     break; 
    case 2: 
     $result[] = array(
    'id' => $o['game_id'], 
    'uriTitle' => $o['game_uriTitle'], 
    'title' => $o['game_title'], 
    ); 
     break; 
    case 4: 
     $result[] = array(
    'comment' => $o['usercomment_comment'], 
    'time' => $o['usercomment_time'], 
    'id' => $o['user_id'], 
    'username' => $o['user_username'], 
    'activate' => $o['user_activate'], 
    ); 
     break; 
    case 5: 
     $result[] = array(
    'id' => $o['game2_id'], 
    'uriTitle' => $o['game2_uriTitle'], 
    'title' => $o['game2_title'], 
    ); 
     break; 
    } 
} 

注意:最後,查詢必須稍微編輯,我只是​​寫了我的腦袋w/o測試。如果我對數據庫結構有更多的瞭解,優化肯定可以完成。

此外,這僅僅是一個概念證明,它確實可以用單個查詢來完成。 ;)

+0

非常令人印象深刻!這將刪除20個查詢,並且只需要0.7秒就可以運行,因此外鍵必須運行良好。 – Dan

+0

很高興知道,不客氣。 ;) – Paul

相關問題