[英]PDO Query from Results of Another PDO Query
I have this code which hits a view in mysql called vAlbums
this returns a JSON array of the query results 我有这段代码会在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) .'}';
}
}
I need to do a sub query to place an array of photos in each album in the array like thus 我需要做一个子查询,将像这样的照片阵列放置在阵列中的每个相册中
{
"albums": [
{
"albu_ablumid": "1",
"photos": [
{
"photourl": "photo1"
},
{
"photourl": "photo2"
},
{
"photourl": "photo3"
}
]
},
{
"albu_ablumid": "2",
"photos": [
{
"photourl": "photo1"
},
{
"photourl": "photo2"
},
{
"photourl": "photo3"
}
]
}
]
}
Can someone show how to achieve this the MySQL query for photos is: 有人可以显示如何实现此MySQL查询照片的方法是:
SELECT * FROM photos WHERE album_id = x
Thanks 谢谢
Why not just do a join and group concat in a single query? 为什么不只在单个查询中加入联接和组?
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
This will return all the photos in a comma separated string in each row that matches the album. 这将在与相册匹配的每一行中以逗号分隔的字符串返回所有照片。 You can then easily split it up as needed in your code. 然后,您可以根据需要轻松地将其拆分为代码。 It saves a lot of connections and multiple queries being run. 这样可以节省大量连接并可以运行多个查询。
Here is a little example from my play db to show how it functions: 这是我的播放数据库中的一个小例子,展示了它的功能:
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)
and with some simple code to take care of the NULLs if you need to for some reason: 并出于一些原因使用一些简单的代码来处理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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.