MYSQL PHP API - 显示和显示从另一个表中获取行内的多行

时间:2013-01-24 11:52:14

标签: php mysql api

我正在为我的移动应用程序创建API。我正在使用PHP MYSQL和Slim框架(这与此问题无关)进行开发。

我正试图从我的mysql数据库中提取多个“场所”,并为每个“场地”检索多个“venue_images”。数据库:

venues        venue_images
------        ------------
id PK         image_venue_id FK (to venues)
venue_name    image_path
active

然后我需要以这种格式输出数据:

{
  "completed_in":0.01068,
  "returned":10,
  "results":[
    {
      "venue_id":"1",
      "venue_name":"NameHere",
      "images": [
         {
           "image_path":"http://www.pathhere.com"
         },
         {
           "image_path":"http://www.pathhere2.com"
         }
      ]
    }
  ]
}

所以基本上,每个场地都会多次迭代图像。

我目前的代码是:

$sql = "
    SELECT
         venues.id, venues.venue_name, venues.active,
         venue_images.image_venue_id, venue_images.image_path
    FROM
         venues
    LEFT JOIN 
         venue_images ON venue_images.image_venue_id = venues.id
    WHERE
         venues.active = 1
    LIMIT 0, 10
    ";

    $data = ORM::for_table('venues')->raw_query($sql, array())->find_many();
         if($data) {
        foreach ($data as $post) {
            $results[] = array (
                 'venue_id' => $post->id,
                 'venue_name' => $post->venue_name,
                 'images' => $post->image_path
            );
        }

        //Build full json
        $time = round((microTimer() - START_TIME), 5);
        $result = array(
             'completed_in' => $time,
             'returned' => count($results),
             'results' => $results
        );
        //Print JSON
        echo indent(stripslashes(json_encode($result)));
    } else {
         echo "Nothing found";
    }

我当前的代码有效,但它产生了这个:

{
  "completed_in":0.01068,
  "returned":10,
  "results":[
    {
      "venue_id":"1",
      "venue_name":"The Bunker",
      "images":"https://s3.amazonaws.com/barholla/venues/1352383950-qPXNShGR6ikoafj_n.jpg"
    },
    {
      "venue_id":"1",
      "venue_name":"The Bunker",
      "images":"https://s3.amazonaws.com/barholla/venues/1352384236-RUfkGAWsCfAVdPm_n.jpg"
    }
]
}

“The Bunker”有两张图片。它不是将图像存储在场地阵列中,而是使用第二张图像创建一个重复的“The Bunker”行。就像我之前说的那样,我需要在每个场地内迭代多个图像。任何帮助将非常感激!谢谢!

2 个答案:

答案 0 :(得分:0)

您想使用GROUP_CONCAT

像这样(可能不是100%准确:))

$sql = "
    SELECT
         v.id, v.venue_name, v.active,
         GROUP_CONCAT(i.image_path) as venue_image_string
    FROM
         venues v
    LEFT JOIN 
         venue_images i ON i.image_venue_id = v.id
    WHERE
         v.active = 1
    GROUP BY i.image_venue_id
    LIMIT 0, 10
";

您可能需要稍微调整一下,但应该让您走上正确的轨道(注意:将venue_image_string设为CSV)

答案 1 :(得分:0)

为什么不能使用多个查询而不是..?它更快更简单..!

$sql = "SELECT venues.id, venues.venue_name, venues.active FROM venues WHERE venues.active = 1 LIMIT 0, 10";

    $data = ORM::for_table('venues')->raw_query($sql, array())->find_many();
         if($data) {
        foreach ($data as $post) {
            $results[] = array ();

$sql = "SELECT image_path FROM venue_images WHERE image_venue_id = $post->id"; 
$images = ORM::for_table('venue_images')->raw_query($sql, array())->find_many();

 $results[] = array (
                 'venue_id' => $post->id,
                 'venue_name' => $post->venue_name, 
                 'images' => $images);
        }

        //Build full json
        $time = round((microTimer() - START_TIME), 5);
        $result = array(
             'completed_in' => $time,
             'returned' => count($results),
             'results' => $results
        );
        //Print JSON
        echo indent(stripslashes(json_encode($result)));
    } else {
         echo "Nothing found";
    }