2013-05-31 175 views
1

我有2个表格。第一个包含一些全局信息,第二个包含图像列表。MySQL:加入一个多行数据表

当我执行这个请求:

SELECT table1.title, table1.description, table2.image LEFT JOIN table2 ON table2.table1_id = table1.table1_id 

表结构:

TABLE1

| table1_id | title | description | 
| 1 | title1 | description1 | 
| 2 | title2 | description2 | 
| 3 | title3 | description3 | 

TABLE2

| id | table1_id | image | 
| 1 | 1 | img/img1.png | 
| 2 | 1 | img/img2.png | 
| 3 | 1 | img/img3.png | 
| 4 | 2 | img/img4.png | 
| 5 | 2 | img/img5.png | 
| 6 | 3 | img/img6.png | 

我有类似的东西:

<?php 
array(
    array('title' => 'title1', 'description' => 'description1', 'image' => 'img/img1.png'), 
    array('title' => 'title1', 'description' => 'description1', 'image' => 'img/img2.png'), 
    array('title' => 'title1', 'description' => 'description1', 'image' => 'img/img3.png'), 
    array('title' => 'title2', 'description' => 'description2', 'image' => 'img/img4.png'), 
    array('title' => 'title2', 'description' => 'description2', 'image' => 'img/img5.png'), 
    array('title' => 'title3', 'description' => 'description3', 'image' => 'img/img6.png') 
); 
?> 

这种结构的问题是重复标题,说明。 我想获得类似的东西:

<?php 
array(
    array('title' => 'title1', 'description' => 'description1', 'image' => 
     array('img/img1.png', 'img/img2.png', 'img/img3.png') 
    ), 
    array('title' => 'title2', 'description' => 'description2', 'image' => 
     array('img/img1.png', 'img/img2.png') 
    ), 
    array('title' => 'title3', 'description' => 'description3', 'image' => 
     array('img/img6.png') 
    ) 
); 
?> 

我的问题是:

  • 是否有可能得到这样的数据结构只是一个SQL请求(没有PHP操纵。 )

  • 如果不是,我必须做什么样的PHP操作来将我的第一个数组转换为第二个数组?

谢谢!

+0

这个问题有点混乱。也许如果你包含你的相关数据库表结构,这些结构可以帮助我[也许还有其他人]理解潜在的问题。 –

+0

我添加了表格结构 – alexmngn

回答

2

看一看group条款和group_concat功能。我不知道是否它会在PHP中的数组,但它几乎是你想要什么:

SELECT table1.title, table1.description, GROUP_CONCAT(table2.image) LEFT JOIN table2 ON table2.id = table1.id GROUP BY table1.id 

可以在PHP中使用explode函数变换GROUP_CONCAT(table2.image)结果到PHP数组

查看文档MySQL's group_concatPHP's explode功能。

+0

@tilix - 这是尽可能接近你想要的。你的PHP输出对每个标题看起来都是这样的:'array('title'=>'title1','description'=>'description1','image'=>'img/img1.png,img/img2.png, IMG/img3.png')'。您可以'爆炸''图像'项目以获取单独的文件名。 –

+0

@EdGibbs - 我几秒前更新了我的答案。无论如何感谢您的评论:-) –

0

您可以对所有不同的(名称,描述)元组进行选择。然后你将得到一个Name,Descriptions和一个空的第三个元素的数组。然后遍历这个数组并获取该独特元组的所有图像。然后取出结果并将其插入到第三个元素中。

查询得到名称/描述的阵列:

select distinct table1.title, table2.description left join table2 on table2.id = table1.id 

查询,找到不同的名称/描述元组的所有图像:

select table2.image inner join table1 on table1.id = table2.id where table1.name = arr[0] and table1.description = arr[1] 
0

当您迭代结果时,您可以非常轻松地构建您所需的结构,无论是您将不得不操作结果集。

<?php 
$items = array(); 
foreach ($rows as $row) { 
    if (! isset($items[ $row['title'] ])) { 
     $items[ $row['title'] ] = array(
      'title'   => $row['title'], 
      'description' => $row['description'], 
      'images'  => array($row['image']), 
     ); 
     continue; 
    } 
    $items[ $row['title'] ]['images'][] = $row['image']; 
} 
$items = array_values($items); 

安东尼。

0
PHP code: 

<?php 
/*here is your result array*/ 
$result = array(
    array('title' => 'tile1', 'description' => 'description1', 'image' => 'img/img1.png'), 
    array('title' => 'tile1', 'description' => 'description1', 'image' => 'img/img2.png'), 
    array('title' => 'tile1', 'description' => 'description1', 'image' => 'img/img3.png'), 
    array('title' => 'tile2', 'description' => 'description2', 'image' => 'img/img4.png'), 
    array('title' => 'tile2', 'description' => 'description2', 'image' => 'img/img5.png'), 
    array('title' => 'tile3', 'description' => 'description3', 'image' => 'img/img6.png') 
); 

/*creating a new formatted array*/ 
$newResult = array(); 
foreach($result as $value){ 
    $newResult[$value['title']]['title'] = $value['title']; 
    $newResult[$value['title']]['description'] = $value['description']; 
    $newResult[$value['title']]['image'][] = $value['image']; 
} 

/*printing new final formatted array*/ 
print_r($newResult)); 
?>