Php MySQL选择多维?
我想从我的数据库中选择所有帖子及其附件 这是具有虚拟数据的结构: 员额表 附件表 到目前为止,我的代码看起来是这样的,但我并没有真正得到我想要的Php MySQL选择多维?,php,mysql,multidimensional-array,Php,Mysql,Multidimensional Array,我想从我的数据库中选择所有帖子及其附件 这是具有虚拟数据的结构: 员额表 附件表 到目前为止,我的代码看起来是这样的,但我并没有真正得到我想要的 $qry = $db->prepare(' SELECT p.id , p.post , p.userId , att.fileName , att.time FROM posts p LEFT JOIN attachments att ON att.postId = p.id ')
$qry = $db->prepare('
SELECT p.id
, p.post
, p.userId
, att.fileName
, att.time
FROM posts p
LEFT
JOIN attachments att
ON att.postId = p.id
');
$qry->execute();
$postsArray = $qry->fetchAll(PDO::FETCH_ASSOC);
我想要这样的东西:
[{'id': 1,
'post': 'hello',
'userId': 1,
'attachments': [{'fileName': 'hey.jpg', 'time:' 0}, ... ]
}, ... ]
$result = $db->query("select id, post, userId from posts");
$posts = [];
while ($post = $result->fetch(PDO::FETCH_OBJECT)) {
$post->attachments = [];
$posts[$post->id] = $post;
}
$result = $db->query("select postId, fileName, time from attachments");
while ($att = $result->fetch(PDO::FETCH_OBJECT)) {
$posts[$att->postId]->attachments[] = $att;
unset($att->postId); // optional
}
$posts = array_values($posts); // optional
echo json_encode($posts);
如何实现这一点?您的查询将为您提供所需结果的正确数据,您可以使用PHP进行后期处理以获得所需格式:
foreach ($postArray as $post) {
$fixed_part = array('id' => $post['id'], 'post' => $post['post'], 'userId' => $post['userId']);
$key = serialize($fixed_part);
if (!isset($out[$key])) $out[$key] = $fixed_part;
$out[$key]['attachments'][] = array('fileName' => $post['fileName'], 'time' => $post['time']);
}
$out = array_values($out);
echo json_encode($out, JSON_PRETTY_PRINT);
输出太长,无法发布,但可以从中看到。查询结果可以在上看到。通常我会这样做:
[{'id': 1,
'post': 'hello',
'userId': 1,
'attachments': [{'fileName': 'hey.jpg', 'time:' 0}, ... ]
}, ... ]
$result = $db->query("select id, post, userId from posts");
$posts = [];
while ($post = $result->fetch(PDO::FETCH_OBJECT)) {
$post->attachments = [];
$posts[$post->id] = $post;
}
$result = $db->query("select postId, fileName, time from attachments");
while ($att = $result->fetch(PDO::FETCH_OBJECT)) {
$posts[$att->postId]->attachments[] = $att;
unset($att->postId); // optional
}
$posts = array_values($posts); // optional
echo json_encode($posts);
请注意,$posts数组是id索引的。我会一直这样。但如果您需要的结果与问题0索引中的结果完全相同,则可以添加以下行:
$posts = array_values($posts);
在较新的MySQL版本中,您还可以通过单个SQL查询获得JSON结果:
select json_arrayagg(post_json) as json
from (
select json_object(
'id', p.id,
'post', p.post,
'userId', p.userId,
'attachments', json_arrayagg(
json_object('fileName', a.fileName, 'time', time)
)
) as post_json
from posts p
left join attachments a on a.postId = p.id
group by p.id
) x
结果:
[{"id": 1, "post": "hello", "userId": 1, "attachments": [{"time": 0, "fileName": "hey.jpg"}, {"time": 53252354, "fileName": "test.png"}]}, {"id": 2, "post": "world", "userId": 1, "attachments": [{"time": 0, "fileName": "asd.png"}]}, {"id": 3, "post": "ouch", "userId": 2, "attachments": [{"time": null, "fileName": null}]}, {"id": 4, "post": "test", "userId": 1, "attachments": [{"time": 0, "fileName": "asd2.png"}]}]
当前查询的结果是什么?要获得这种格式的结果,只需循环实际返回数据,并使用类似的子数组构建一个新数组。没有真正的“one query Dos it”操作可以使用该格式:没有真正的“one query Dos it”操作可以使用该格式@IncredibleHat,因为预期的结果有点像有效的JSON,MySQL有可以使用JSON的JSON函数。。topicstarter的问题是你想在MySQL服务器上还是在PHP端生成JSON。我无法想象为什么有人会选择在MySQL中而不是在应用程序中进行数组解析code@RaymondNijland那很酷。第一个支持json的Mysql版本还没有这些聚合函数。很高兴知道他们来了。谢谢你的帮助!我坚持使用这个解决方案,在阅读了几分钟之后,我意识到这个方法可能最适合当前的代码环境。