1

私は2つのテーブルを持っています。最初のファイルにはグローバル情報が含まれ、2 番目のファイルには画像のリストが含まれています。

このリクエストを実行すると:

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

テーブル構造:

表1

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

表2

| 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操作なし..)

  • そうでない場合、最初の配列を 2 番目の配列に変換するには、どのような PHP 操作を行う必要がありますか?

ありがとう!

4

4 に答える 4

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で関数を使用して、結果をPHP配列explodeに変換できますGROUP_CONCAT(table2.image)

MySQL の group_concatPHP の爆発関数のドキュメントを参照してください。

于 2013-05-31T18:34:25.827 に答える
0

すべての個別の (名前、説明) タプルを選択できます。次に、Name、Descriptions、および null の 3 番目の要素の配列を取得します。次に、この配列をループして、その個別のタプルのすべての画像を取得します。次に、その結​​果を取得して、3 番目の要素に挿入します。

名前/説明の配列を取得するクエリ:

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]
于 2013-05-31T18:34:02.617 に答える
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));
?>
于 2013-05-31T18:44:49.540 に答える
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);

アンソニー。

于 2013-05-31T18:40:09.263 に答える