gpt4 book ai didi

mysql - SQL-按特定字段分组并连接另一个字段

转载 作者:行者123 更新时间:2023-11-29 09:56:21 25 4
gpt4 key购买 nike

我有一个返回下表的查询:

   P_id   S_id   Time
1 "20" A 15
2 "30" B 50
3 "50" A 99
4 "70" A 60

我想根据“Sid”列对表进行分组,并按“Time”列排序,因此它看起来像这样:

        P_id       S_id   
1 "20","70","50" A
2 "30" B

通过更改 SQL 查询来实现此目的的最佳方法是什么?

当尝试添加“GROUP BY S_id”时,出现错误:

SELECT list expression references column query which is neither grouped nor aggregated at [2:16]

(意味着它不知道如何对 P_id 的值(所有字符串)进行分组

最佳答案

我想你想要:

select s_id, group_concat(p_id order by time) as p_ids
from t
group by s_id;

如果您想要第一列包含数字,您可以将其添加到:

select (@rn := @rn + 1) as seqnum, s_id, group_concat(p_id order by time) as p_ids
from t cross join
(select @rn := 0) params
group by s_id;

关于mysql - SQL-按特定字段分组并连接另一个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53763997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com