我想從我的數(shù)據(jù)庫中選擇所有帖子及其附件。這是帶有虛擬數(shù)據(jù)的結(jié)構(gòu):帖子表id | post | userId | ---------|---------------|----------|1 | "hello" | 1 |2 | "world" | 1 |3 | "ouch" | 2 |4 | "test" | 1 |附件表id | postId | fileName | time | ---------|---------------|----------|----------|1 | 1 |"hey.jpg" | 0 |2 | 1 |"test.png"| 53252354 |3 | 2 |"asd.png" | 0 |4 | 4 |"asd2.png"| 0 |到目前為止,我的代碼看起來像這樣,但我并沒有真正得到我想要的。$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}, ... ]}, ... ]我怎么能做到這一點(diǎn)?
MySQL選擇多維?
寶慕林4294392
2021-07-02 14:00:44