连接后在groupconcat中按日期分组
Grouping by date in groupconcat after concatenation
我有一个商店 table 中的商店列表,我还有一个商店评论 table,其中包含每个商店的多个评论。它们通过 storeID 外键链接。
这是一个更大查询的子查询,可将多条信息 return 放入按每个商店排序的单个行中。此子查询的目的是 return 该商店的所有评论以 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
最后我尝试将日期时间转换为 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
我确定有一种简单的方法可以解决这个问题,但我就是看不到。有没有人有什么建议?
您可以对 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)
我有一个商店 table 中的商店列表,我还有一个商店评论 table,其中包含每个商店的多个评论。它们通过 storeID 外键链接。
这是一个更大查询的子查询,可将多条信息 return 放入按每个商店排序的单个行中。此子查询的目的是 return 该商店的所有评论以 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
最后我尝试将日期时间转换为 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
我确定有一种简单的方法可以解决这个问题,但我就是看不到。有没有人有什么建议?
您可以对 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)