开发者

MySQL GROUP_CONCAT headache

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

Le开发者_如何学运维t 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")


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜