Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从组_concat mysql的结果中获取最大值_Mysql_Sql_Greatest N Per Group - Fatal编程技术网

如何从组_concat mysql的结果中获取最大值

如何从组_concat mysql的结果中获取最大值,mysql,sql,greatest-n-per-group,Mysql,Sql,Greatest N Per Group,我正试图从group_concat的结果中获得最大值 这是样本数据和我尝试过的 +----+---------+--------------------------+ | id | user_id | comment | +----+---------+--------------------------+ | 1 | 80 | I don't need it any more | | 2 | 222 | Don't need this a

我正试图从group_concat的结果中获得最大值

这是样本数据和我尝试过的

+----+---------+--------------------------+
| id | user_id | comment                  |
+----+---------+--------------------------+
|  1 |      80 | I don't need it any more |
|  2 |     222 | Don't need this account  |
|  3 |      80 | I have an other account  |
|  4 |      80 | The other comment        |
|  5 |     222 | some x                   |
+----+---------+--------------------------+
4 rows in set (0.01 sec)
我尝试了以下查询

mysql> select max(group_concat(id  SEPARATOR ' ')), user_id from userapp_accactivitylog;
但它给了我错误

ERROR 1111 (HY000): Invalid use of group function
使用
group_concat(id)
我们将得到结果
1,2,3,4,5
。我的要求是,我想从group_concat获得的结果中选择最大的数字

我希望您理解我的要求,请告诉我是否有办法实现以下结果:

+----+---------+--------------------------+
| id | user_id | comment                  |
+----+---------+--------------------------+
|  5 |     222 | some x                   |
|  4 |      80 | The other comment        |
+----+---------+--------------------------+
2 rows in set (0.01 sec)

我正在尝试按用户id进行分组,我希望获取id列中最大数量的记录(即在本例中为5条)

这应该会得到你想要的:

select
  user_id,
  max(id) as id,
  substring_index(group_concat(comment order by id desc SEPARATOR '|'), '|', 1)
from
  userapp_accactivitylog
group by user_id;

这将得到您想要的:

select
  user_id,
  max(id) as id,
  substring_index(group_concat(comment order by id desc SEPARATOR '|'), '|', 1)
from
  userapp_accactivitylog
group by user_id;

这是一个经常被问到的问题的变体

这里有一个解决方案:

SELECT u.*
FROM (
    SELECT user_id, MAX(id) AS id 
    FROM userapp_accactivitylog 
    GROUP BY user_id) AS t
JOIN userapp_accactivitylog AS u USING (id)

这是一个经常被问到的问题的变体

这里有一个解决方案:

SELECT u.*
FROM (
    SELECT user_id, MAX(id) AS id 
    FROM userapp_accactivitylog 
    GROUP BY user_id) AS t
JOIN userapp_accactivitylog AS u USING (id)

为什么不直接使用
max(用户id)
?你能编辑你的问题并提供想要的结果吗?我知道max(用户id),但在我的用例中,我需要像我这样的东西trying@naveentamanam但是为什么呢-(@草莓:我得到了我想要的,“Gordon Linoff”给了我我想要的完美答案。@naveentamanam的问题和答案对整个社区都有好处,所以如果你能解释为什么你认为你需要这个非传统的解决方案,那就很有帮助了。为什么不直接使用
max(用户id)
?您能编辑您的问题并提供所需的结果吗?我知道max(用户id),但在我的用例中,我需要类似我的东西trying@naveentamanam但是为什么?:-(@草莓:我得到了我想要的,“戈登·林诺夫”给了我我想要的完美答案。@naveentamanam的问题和答案对整个社区都有好处,所以如果你能解释一下为什么你认为你需要这个非常规的解决方案,那就很有帮助了。谢谢,看起来很有效。我已经编辑了你给出的查询,因为关于group_concat有语法错误。@naveentamanam…谢谢。无论我使用多少次,我仍然认为order by之前有分隔符。谢谢,它似乎有效。我已经编辑了您给出的查询,因为group_concat存在语法错误。@Naveentamananam…谢谢。无论我使用多少次,我仍然认为order by之前有分隔符。