我有一个包含两个表的MySQL数据库:相册和照片。这是专辑表的样子:
+---------+-------------+---------------------------+--------+
| albumID | albumName | albumDescription | userID |
+---------+-------------+---------------------------+--------+
| 1 | Winter 2018 | Pictures from winter time | 1 |
| 2 | Spring 2018 | Pictures from spring time | 1 |
| 3 | Summer 2018 | Pictures from summer time | 1 |
| 4 | Fall 2018 | Pictures from fall time | 1 |
+---------+-------------+---------------------------+--------+
照片表如下:
+---------+---------+---------------+------------+
| photoID | albumID | photoCategory | photoURL |
+---------+---------+---------------+------------+
| 1 | 1 | dog | photo1.jpg |
| 2 | 1 | cat | photo2.jpg |
| 3 | 1 | dog | photo3.jpg |
| 4 | 4 | dog | photo4.jpg |
+---------+---------+---------------+------------+
我现在返回与专辑ID SELECT * FROM photos WHERE albumID = 1
相关联的照片。检索node.js中的数据时,将返回所有三张照片的信息,如下所示:
[{"photoID":1,"albumID":1,"photoCategory":"dog","photoURL":"photo1.jpg"},
{"photoID":2,"albumID":1,"photoCategory":"cat","photoURL":"photo2.jpg"},
{"photoID":3,"albumID":1,"photoCategory":"dog","photoURL":"photo3.jpg"}]
但是,我正在寻找一种根据类别将结果分组在一起的方法。我想实现以下目标:
["photoCategory":"dog" = [
{"photoID":1,"albumID":"1","photoURL":"photo1.jpg"},
{"photoID":3,"albumID":"1","photoURL":"photo3.jpg"}],
"photoCategory":"cat" = [
{"photoID":2,"albumID":"1","photoURL":"photo2.jpg"}]]
我想知道用MySQL是否可以实现? 如果没有,我可以在node.js中过滤结果以达到相同的结果吗?