Using LIMIT with GROUP_CONCAT in MariaDB 10.3.3+
Contents
Prior to version 10.3.3 of MariaDB it was not possible to use LIMIT
in a GROUP_CONCAT
query. So in cases you wanted to select the first n elements of a GROUP_CONCAT
query you had to make use of SUBSTRING_INDEX
.```
CREATE TABLE t1 (id BIGINT NOT NULL AUTO_INCREMENT, email VARCHAR(255), PRIMARY KEY(id));
INSERT INTO t1 (email) VALUES (‘email’), (‘test2@email.com’), (‘test1@email.com’);
To select the first element you would execute the following query:
SELECT SUBSTRING_INDEX(GROUP_CONCAT(email SEPARATOR ‘::'), ‘::’, n) FROM t1;
where n is the number of elements required
This has always felt hacky to me. Now you can make the query simple and straightforward by using:
SELECT GROUP_CONCAT(email SEPARATOR ‘::’ LIMIT n) FROM t1;