将mysql查询与子查询结果合并为一个PHP数组

时间:2012-05-03 16:56:38

标签: php arrays

我有一个包含事件的表,列出这些事件我循环遍历事件表,检查事件类型并在它的特定表中查找它的值,并使用它的eventId。

目前,这使用一个查询来获取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

然后我将能够访问数据,并且只是通过它循环。

我只是不确定将多个结果集合并到数组中的最佳方法?

或者你可以尝试将它们组合成一个查询...

$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,这是在确定最小化此方法的最佳方法之后的下一步。

1 个答案:

答案 0 :(得分:1)

接受挑战。 ;)

由于您实际上只对while循环中的结果感兴趣,因此您可以尝试此单个查询。由于LEFT JOINS它可能不会更快,几乎取决于您的数据库。最终$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测试。如果我对db结构有更多了解,肯定可以进行优化。

此外,这仅仅是一个概念证明,它确实可以通过单个查询来完成。 ;)