PDO查询来自另一个PDO查询的结果

时间:2012-09-13 11:18:38

标签: php mysql json pdo

我有这个代码在mysql中命中一个名为vAlbums的视图,这会返回查询结果的JSON数组

function getAlbumPics($arno){
    $aSql = "SELECT albu_ablumid, albu_name_en, albu_name_de, albu_name_fr, albu_name_nl, albu_name_es, albu_name_it, albu_photourl FROM vAlbums WHERE site_arno=:arno";
    try {
        $db = getConnection();
        $aStmt = $db->prepare($aSql);
        $aStmt->bindParam(":arno",$arno);
        $aStmt->execute();
        $albums = $aStmt->fetchAll(PDO::FETCH_OBJ);
        $arrAID = $aStmt->fetchColumn(2);
        $db = null;
        echo '{"albums": ' . json_encode($albums) . '}';
    } catch(PDOException $e) {
        echo '{"error":[{"text":"'. $e->getMessage() .'"}],';
        echo '"SQL": ' . json_encode($aSql) .'}';
    }
}

我需要做一个子查询,将数组中的照片放在数组中,如此

{
    "albums": [
        {
            "albu_ablumid": "1",
            "photos": [
                {
                    "photourl": "photo1"
                },
                {
                    "photourl": "photo2"
                },
                {
                    "photourl": "photo3"
                }
            ]
        },
        {
            "albu_ablumid": "2",
            "photos": [
                {
                    "photourl": "photo1"
                },
                {
                    "photourl": "photo2"
                },
                {
                    "photourl": "photo3"
                }
            ]
        }
    ]
}

有人可以展示如何实现这一点,MySQL的照片查询是:

SELECT * FROM photos WHERE album_id = x

由于

1 个答案:

答案 0 :(得分:1)

为什么不在一个查询中进行连接和组连接?

SELECT 
    albu_ablumid, 
    albu_name_en, 
    albu_name_de, 
    albu_name_fr, 
    albu_name_nl, 
    albu_name_es, 
    albu_name_it, 
    albu_photourl,
    group_concat(photourl) // Guessing at column Name
FROM 
    vAlbums a
        join photos b
            on a.albu_ablumid=b.album_id
WHERE 
    site_arno=:arno
group by
    albu_ablumid, 
    albu_name_en, 
    albu_name_de, 
    albu_name_fr, 
    albu_name_nl, 
    albu_name_es, 
    albu_name_it, 
    albu_photourl

这将返回与相册匹配的每行中逗号分隔的字符串中的所有照片。然后,您可以根据需要在代码中轻松地将其拆分。它可以节省大量连接并运行多个查询。

这是我的播放数据库中的一个小例子,展示它的功能:

mysql> select * from table1;
+---------+------+------+-------------+
| autonum | ID   | name | metavalue   |
+---------+------+------+-------------+
|       1 |    1 | Rose | Drinker     |
|       2 |    1 | Rose | Nice Person |
|       3 |    1 | Rose | Runner      |
|       4 |    2 | Gary | Player      |
|       5 |    2 | Gary | Funny       |
|       6 |    2 | Gary | NULL        |
|       7 |    2 | Gary | Smelly      |
+---------+------+------+-------------+
7 rows in set (0.00 sec)

mysql> select ID, group_concat(metavalue) from table1 group by ID;
+------+----------------------------+
| ID   | group_concat(metavalue)    |
+------+----------------------------+
|    1 | Drinker,Nice Person,Runner |
|    2 | Player,Funny,Smelly        |
+------+----------------------------+
2 rows in set (0.00 sec)

并使用一些简单的代码来处理NULL,如果您出于某种原因需要:

mysql> select ID, group_concat(coalesce(metavalue,'Empty')) from table1 group by ID;
+------+-------------------------------------------+
| ID   | group_concat(coalesce(metavalue,'Empty')) |
+------+-------------------------------------------+
|    1 | Drinker,Nice Person,Runner                |
|    2 | Player,Funny,Empty,Smelly                 |
+------+-------------------------------------------+
2 rows in set (0.00 sec)