0

我正在运行这个 SQL 查询

$sql = "select images.image, images.comment as feedDescription, 
        customers.fullName, CONCAT('[', GROUP_CONCAT(DISTINCT likes.uid),']') as likes, 
        CONCAT('[', GROUP_CONCAT(DISTINCT CONCAT('{\"userid\":\"', comments.fid, '\", \"comment\":\"', comments.comment, '\"}') separator ','),']') as comments 
        FROM images 
        LEFT JOIN customers on images.client_id = customers.client_id 
        LEFT JOIN likes on images.image = likes.image 
        LEFT JOIN comments on images.image = comments.image 
        WHERE images.fid=:userID 
        ORDER BY images.image LIMIT $offset,$limit";

唯一的问题是我只得到第一行......

我有图像表、客户表(通过我在图像中获得的 id 取客户的姓名)、喜欢表(在图像上“点赞”的人)和评论(在表上写“评论”的人)

4

1 回答 1

1

您在查询中使用聚合函数,因此 MySQL 自动只返回一行——所有数据的聚合。

在其他数据库中,这会产生错误,因为您混合了聚合列和非聚合列。这是 MySQL 的一个(错误)功能,称为“隐藏列”。

将 group by 添加到您的查询中以解决问题:

group by images.image, images.comment, customers.fullName

请务必在 WHERE 子句之后和 ORDER BY 之前添加它。

于 2012-08-28T14:23:06.640 回答