php mysql select,数组结果里面的数组结果

时间:2012-02-08 10:57:24

标签: php mysql multidimensional-array

我有3张桌子,

 tbl_photo               tbl_photo_comments      tbl_photo_likers
 ___________             ____________             _____________
| photo_id  |           | comment_id |           | like_id     |
| photo_url |           | photo_id FK|           | user_id FK  |
| user_id FK|           | user_id FK |           | photo_id FK |
                        | comment    | 

我的目标是从tbl_photo获取照片数据及其各自的评论数据和喜欢的数据。我想要的数组的结构如下所示,我有一个结果数组,其中有2个数组作为其数据的元素

oneResultArray =
{
  photo_url = "www.url.com/photo.png";
  photoID = 1;
  user_id =  2
  commentData = (
     {
         comment = "comment 1";
         userid = 1
     },
     {
         comment = "comment 2";
         userid = 2
     },
     {
         comment = "comment 3";
         userid = 3});

     likersData = (
     {
         userid = 2;
         username = liker1;
     },
     {
         userid = 3;
         username = liker2;
     });

   },
{
  photo_url = "www.url.com/photo.png";
  photoID = 1;
  user_id =  2
  commentData = (
     {
         comment = "comment 1";
         userid = 1
     },
     {
         comment = "comment 2";
         userid = 2
     },
     {
         comment = "comment 3";
         userid = 3});

     likersData = (
     {
         userid = 2;
         username = liker1;
     },
     {
         userid = 3;
         username = liker2;
     });
   }

我的问题是,是否可以使用mysql上的一个查询来完成此操作?如果没有,还有其他方法吗?谢谢你们!

3 个答案:

答案 0 :(得分:4)

正如davidethell指出的那样,你不想加入这些表格。因此,无法在单个查询中选择您的数据。 Garry Welding的方法可能会被解释为对您获得的每张照片记录进行后续查询。这是 NOT 您想要做的事情。 3张照片将导致7个查询被执行。这超过了必要的4。 10张照片将导致21次查询。你正在拍照。尝试以下方面:

<?php

// build hierarchical result
$result = array();

// getting the photos
$query = $pdo->query('SELECT photo_id, photo_url, user_id FROM tbl_photo WHERE user_id = 5');
foreach ($query as $row) {
    $result[$row['photo_id']] = $row;
    $result[$row['photo_id']]['comments'] = array();
    $result[$row['photo_id']]['likes'] = array();
}

if ($result) {
    // comments and likes only for the photos we've selected
    $photos = join(',', array_keys($result));

    // getting the comments
    $query = $pdo->query('SELECT comment_id, photo_id, user_id, comment FROM tbl_photo_comments WHERE photo_id IN (' . $photos . ')');
    foreach ($query as $row) {
        $result[$row['photo_id']]['comments'][$row['comment_id']] = $row;
    }

    // getting the likes
    $query = $pdo->query('SELECT like_id, user_id, photo_id FROM tbl_photo_likers WHERE photo_id IN (' . $photos . ')');
    foreach ($query as $row) {
        $result[$row['photo_id']]['likes'][$row['like_id']] = $row;
    }
}

var_dump($result);

答案 1 :(得分:1)

如果每行中没有大量重复数据,则无法在一个查询中执行此操作。在三个查询中执行此操作将更有效,更容易。您将查询照片表,循环浏览它,并在循环的每次迭代中执行两个查询,一个用于评论,一个用于喜欢。

答案 2 :(得分:-1)

$photos = {code to get the array of photos};

foreach($photos as &$photo) {
    $photo['comments'] = {code to get photo comments using the photo id};
    $photo['likes'] = {code to get photo likes using the photo id}
}

return $photos;

这是我所知道的唯一方法。 &amp;在$ photo之前是通过引用传递数组。通过引用传递数组仅适用于PHP5 +,http://php.net/manual/en/language.types.array.php

从上面的PHP.net链接:

// PHP 5
foreach ($colors as &$color) {
    $color = strtoupper($color);
}
unset($color); /* ensure that following writes to
$color will not modify the last array element */

// Workaround for older versions
foreach ($colors as $key => $color) {
    $colors[$key] = strtoupper($color);
}

print_r($colors);