在我的SQL中排序组之前

时间:2012-09-27 16:20:59

标签: mysql sorting group-by group-concat

我正在尝试在分组之前对表(注释)进行排序,因此我创建了一个名为comm_sorted的临时表,并按时间戳对其进行排序。

然而,它仍然不起作用。

    $sql = "
select images.id,images.image as imageURL, images.fid as userID, user.name as userFullName, images.comment as feedDescription, images.sharedServices, stores.name as customerName, 
CONCAT('[', 
GROUP_CONCAT(
DISTINCT CONCAT('\"',likes.uid,'\"')),']') as likes , 
CONCAT('[', 
GROUP_CONCAT(
DISTINCT CONCAT('{\"userId\":\"',comm_sorted.uid,'\",\"comment\":\"',comm_sorted.comment,'\",\"timeStamp\":\"',comm_sorted.timestamp,'\"}') separator ','),']')  as comments FROM images 
LEFT JOIN stores on images.client_id=stores.id 
LEFT JOIN likes on images.id=likes.feedid 
LEFT JOIN (select * from comments order by timestamp DESC) as comm_sorted on images.id=comm_sorted.feedid  
LEFT JOIN user on images.fid=user.id WHERE images.fid=:userID group by images.id, user.name, images.image, images.comment, images.sharedServices, stores.name order by images.Timestamp 
DESC LIMIT $offset, $limit";

1 个答案:

答案 0 :(得分:1)

感谢Barmar先生 - 您可以在GROUP_CONCAT()函数中放置ORDER BY comm_sorted.timestamp选项。不需要临时查询。问题已经结束。