【问题标题】:Grouping by date in groupconcat after concatenation连接后在 groupconcat 中按日期分组
【发布时间】:2015-04-30 15:32:14
【问题描述】:

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

这是一个更大查询的子查询,用于将多条信息返回到按每个商店排序的单行中。此子查询的目的是将该商店的所有 cmets 返回到每个商店的单个列中,格式为 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

这主要是可行的,但是 cmets 的排序失败并且它按照输入的顺序出现。

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

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

最后我尝试将日期时间转换为 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

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

【问题讨论】:

    标签: mysql group-concat


    【解决方案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)
    

    【讨论】:

    • 魔术。我从不认为这是正确的方法。我会在允许的时候接受。谢谢!
    猜你喜欢
    • 2018-05-10
    • 2021-12-06
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多