My mistakes

Archive for September 2011

create table test_table(id integer unsigned,cName varchar(50));

insert into test_table values(1,’test1′);
insert into test_table values(2,’test2′);
insert into test_table values(3,’test3′);

select group_concat(cName separator ‘,’)
from test_table

+———————————-+
| group_concat(cName separator ‘,’) |
+———————————-+
| test1,test2,test3 |
+———————————-+

But now,I want to limit the group_concat to the first two rows of the result:

+———————————-+
| group_concat(name separator ‘,’) |
+———————————-+
| test1,test2 |
+———————————-+

Solution :
We cannot use the limit with the group_concat so the better solution is to go for SUBSTRING_INDEX like

SELECT
SUBSTRING_INDEX(group_CONCAT(cName) , ‘,’, 2) as cName from test_table

Advertisements
Tags: