0

我正在尝试创建一个查询,以分类方式返回用户的所有图像,因此响应如下所示:

{
     totalAvatarImgs: 6,
     totalCoverImgs: 2,
     totalPostImgs: 0,
     avatarImgs: [{
         id: 1,
         img: "avatar-1.jpg"
     },{
         id: 2,
         img: "avatar-2.jpg"
     },{
         id: 3,
         img: "avatar-3.jpg"
     }],
     coverImgs: [{
         id: 4,
         img: "cover-1.jpg"
     },{
         id: 5,
         img: "cover-2.jpg"
     }],
     postImgs: []
 }

这是我的images&image_categories表结构:

在此处输入图像描述

查询看起来像这样,但它抛出Error Code: 1140. In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column 'db.images.id'; this is incompatible with sql_mode=only_full_group_by

SELECT 
    a.total AS totalAvatarImgs,
    c.total AS totalCoverImgs,
    p.total AS totalPostImgs,
    CONCAT('[', GROUP_CONCAT(DISTINCT (JSON_OBJECT('id', a.id, 'img', a.img))), ']') AS avatarImgs,
    CONCAT('[', GROUP_CONCAT(DISTINCT (JSON_OBJECT('id', c.id, 'img', c.img))), ']') AS coverImgs,
    CONCAT('[', GROUP_CONCAT(DISTINCT (JSON_OBJECT('id', p.id, 'img', p.img))), ']') AS postImgs
FROM image_categories AS ic
    LEFT JOIN images AS i ON i.imgCatID = ic.id
    JOIN (SELECT id, img, COUNT(*) AS total FROM images WHERE imgCatID = 2 AND uID = 10 LIMIT 3) AS a
    JOIN (SELECT id, img, COUNT(*) AS total FROM images WHERE imgCatID = 3 AND uID = 10 LIMIT 3) AS c
    JOIN (SELECT  id, img, COUNT(*) AS total FROM images WHERE imgCatID = 1 AND uID = 10 LIMIT 3) AS p
WHERE i.uID = 10;

我应该如何重构此查询以使其在严格模式下工作?

先感谢您!

4

0 回答 0