{@ 44}}在groupconcat中按日期分组

时间:2015-04-30 15:32:14

标签: mysql group-concat

我在商店表中有一个商店列表,我还有一个商店评论表,其中包含每个商店的多个评论。它们通过storeID外键链接。

这是一个子查询,用于更大的查询,将多条信息返回到按每个商店排序的单行中。此子查询的目的是将每个商店的所有注释返回到每个商店的单个列,格式为yyyy-mm-dd - COMMENT - Name,并在顶部以最新注释排序。我可以让字符串工作,但总是无法订购。

LEFT JOIN (SELECT group_concat(storecomment.CommentDate, ' - ', storecomment.Comment, ' - ', users.Name, '\n' SEPARATOR '') as storecomments, StoreID from storecomment
                       inner join users on storecomment.CommentUserID = users.UserID 
                       ORDER BY CommentDate DESC
                       group by StoreID) 
                       as storecomments on store.StoreID = storecomments.StoreID

这大部分都有效,但是评论的顺序失败了,它出现在输入的顺序中。

我也试过按顺序字符串的第一部分排序,例如:

LEFT JOIN (SELECT group_concat(storecomment.CommentDate, ' - ', storecomment.Comment, ' - ', users.Name, '\n' SEPARATOR '') as storecomments, StoreID from storecomment
                       inner join users on storecomment.CommentUserID = users.UserID
                       group by StoreID
                       Order by UNIX_TIMESTAMP(SUBSTRING(storecomments,9)) DESC)
                       as storecomments on store.StoreID = storecomments.StoreID

最后我尝试将datetime转换为unix时间戳并按此顺序排序,但仍无法订购:

LEFT JOIN (SELECT group_concat(storecomment.CommentDate, ' - ', storecomment.Comment, ' - ', users.Name, '\n' SEPARATOR '') as storecomments, StoreID from storecomment
                        inner join users on storecomment.CommentUserID = users.UserID
                        group by StoreID
                        Order by UNIX_TIMESTAMP(STR_TO_DATE(storecomment.CommentDate, '%Y-%m-%d %h:%i%p')) DESC
                        as storecomments on store.StoreID = storecomments.StoreID

我确信有一种简单的方法可以解决这个问题,但我看不到它。有没有人有什么建议?

1 个答案:

答案 0 :(得分:1)

您可以在group_concat内按顺序排列值的连接方式,因为它内置了对order by的支持。

将您的group_concat更改为:

group_concat(storecomment.CommentDate, ' - ', storecomment.Comment, ' - ', users.Name, '\n' ORDER BY storecomment.CommentDate DESC SEPARATOR '')

示例:

mysql> create table example (user_id integer, group_id integer);
Query OK, 0 rows affected (0.10 sec)

mysql> insert into example values (1, 1), (1, 2), (1, 3), (2, 7), (2, 4), (2, 5);
Query OK, 6 rows affected (0.07 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select group_concat(group_id) from example group by user_id;
+------------------------+
| group_concat(group_id) |
+------------------------+
| 1,2,3                  |
| 7,4,5                  |
+------------------------+
2 rows in set (0.00 sec)

mysql> select group_concat(group_id order by group_id asc separator '-') from example group by user_id;
+------------------------------------------------------------+
| group_concat(group_id order by group_id asc separator '-') |
+------------------------------------------------------------+
| 1-2-3                                                      |
| 4-5-7                                                      |
+------------------------------------------------------------+
2 rows in set (0.00 sec)