我有这个代码,它在 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
谢谢