Bug #30098 limit support for group_concat
Submitted: 27 Jul 2007 13:29 Modified: 27 Jul 2007 14:03
Reporter: Olaf van der Spek (Basic Quality Contributor) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: DML Severity:S4 (Feature request)
Version:* OS:Any
Assigned to: CPU Architecture:Any
Tags: qc

[27 Jul 2007 13:29] Olaf van der Spek
Description:
Hi,

group_concat does support order by, but not limit. I'd like to only include the first 24 rows in the concat, but that's not possible currently.

Current syntax:
GROUP_CONCAT([DISTINCT] expr [,expr ...]
             [ORDER BY {unsigned_integer | col_name | expr}
                 [ASC | DESC] [,col_name ...]]
             [SEPARATOR str_val])

How to repeat:
select distinct pid from xcl_prev_players where pid != ? and lid = ? order by points desc limit 24

select group_concat(distinct pid order by points desc limit 24) from xcl_prev_players where pid != ? and lid = ?
[27 Jul 2007 13:38] MySQL Verification Team
Thank you for the bug report feature request.
[27 Jul 2007 14:03] Olaf van der Spek
And if you do truncate the result, please don't include part of a value.
[25 Oct 2007 0:02] Dan Kloke
Agreed, this would be a very useful (and used) feature.

Similar to 12544, submitted back in 2005-08-12.
[25 Oct 2007 1:16] Dan Kloke
There are cases where this can be done with clever use of delimiters and SUBSTRING_INDEX, and maybe some REPLACE thrown in.

But sometimes not.
[19 Oct 2009 14:27] Valeriy Kravchuk
Bug #12544 was marked as a duplicate of this one.
[12 May 2015 13:45] Yehuda Deutsch
Any chance this issue will get some attention?
This can be solved either by allowing to set 'group_concat_max_len' with any value starting from 1, or adding LIMIT to GROUP_CONCAT.

Thanks
[15 May 2015 15:06] Gerrit HOEKSTRA
8 years on and still no progress on such a simple, yet language-enriching enhancement?
[19 Mar 2017 18:10] Rick James
Just another vote for this feature.  And another example of the need: http://stackoverflow.com/questions/42789101/mysql-fast-check-if-hash-exists
[30 Sep 2021 7:52] soheil rahsaz
I also vote for this feature and find it very useful.
[19 Mar 2022 20:14] Andres Ferrando
+1 vote. And another complementary idea: add a different separator for the last element.
Sample potential syntax and usage: "GROUP_CONCAT(DISTINCT elem SEPARATOR ', ' FINAL_SEPARATOR ' & ')" can then produce a result like "Curly, Larry & Moe"
[19 May 23:53] Craig Francis
As the use of SQL_CALC_FOUND_ROWS is now deprecated, it would be good if the COUNT() and limited range of IDs could be returned in a single query, so a subsequent query can do a much faster `id IN (?,?,?)`:

SELECT
  COUNT(DISTINCT id) AS c,
  GROUP_CONCAT(DISTINCT id ORDER BY name ASC LIMIT ?, ? SEPARATOR ",") AS ids
FROM
  ...