views:

568

answers:

2

For performance,I need to set a limit for the GROUP_CONCAT,

and I need to know if there are rows not included.

How to do it?

EDIT

Let me provide a contrived example:

create table t(qid integer unsigned,name varchar(30));

insert into t value(1,'test1');    
insert into t value(1,'test2');    
insert into t value(1,'test3');

select group_concat(name separator ',') 
  from t 
 where qid=1;

+----------------------------------+
| group_concat(name separator ',') |
+----------------------------------+
| test1,test2,test3                |
+----------------------------------+

But now,I want to group 2 entries at most,and need to know if there is some entry not included in the result:

+----------------------------------+
| group_concat(name separator ',') |
+----------------------------------+
| test1,test2                      |
+----------------------------------+

And I need to know that there is another entry left(in this case it's "test3")

A: 

How are you going to set the limit? And what performance issues will it solve?

You can get the number of rows in a group using count(*) and compare it to the limit.

Lukáš Lalinský
@Lukáš Lalinský,sorry for my late clarification!
Misier
+1  A: 

this should do the trick

SELECT 
SUBSTRING_INDEX(group_CONCAT(name) , ',', 2) as list , 
( if(count(*) > 2 , 1 , 0)) as more
 FROM 
   t 
 WHERE 
   qid=1
Sabeen Malik
No,this is indeed a "trick" that does more work to retrieve less,which is not what I want.
Misier
limit in group_concat is not supported at this point , any query based solution will do more work than a simple function call like that one.
Sabeen Malik
This is probably as close as you can get, and is relatively efficient if you want to get a set data size (e.g. exactly a certain number of values) from the group_concat; but not so much if you want it to behave like a limit.
Aea