How to hack MySQL GROUP_CONCAT to fetch a limited number of rows?

Misier picture Misier · Oct 5, 2009 · Viewed 17.1k times · Source

I somehow need this feature,but MySQL doesn't support it at this moment.

I'm using GROUP_CONCAT(CONCAT(...)) to generate a xml-like stuff.

But when the size exceeds the limit,the xml is just broken!

So I have to somehow make it only retrieve 5 rows !

Answer

Denis Dupere picture Denis Dupere · Jun 11, 2012

I've worked around this using SUBSTRING_INDEX.

For example:

SELECT SUBSTRING_INDEX(GROUP_CONCAT(Field1 SEPARATOR ','), ',', [# of elements to return])
FROM Table1;