Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL GROUP_CONCAT doesn't return all results

i've used GROUP_CONCAT to get results splitted by comma (,), but when i saw, the GRUP_CONCAT returned only 205 splitted numbers, but in the database there's 2448 results (different aid). Here is my query:

SELECT GROUP_CONCAT(`aid`) As favoriti 
FROM `z_web_favoriti` 
WHERE `kup_id`='1' AND `pos_id`='571'

When i execute:

SELECT DISTINCT `aid` 
FROM `z_web_favoriti` 
WHERE `kup_id`='1' AND `pos_id`='571'

I get the following result: Showing rows 0 - 29 (2448 total,..)

Anyone has some solution why it isn't working? I've searched on stackoverflow for similar problem, but i couldn't find it..

like image 465
Ultrazz008 Avatar asked Feb 28 '17 12:02

Ultrazz008


People also ask

Is there a length limit to Group_concat?

I'm using GROUP_CONCAT() in a MySQL query to convert multiple rows into a single string. However, the maximum length of the result of this function is 1024 characters.

What is the difference between concat and Group_concat in MySQL?

The difference here is while CONCAT is used to combine values across columns, GROUP_CONCAT gives you the capability to combine values across rows. It's also important to note that both GROUP_CONCAT and CONCAT can be combined to return desired results.

What does Group_concat do in MySQL?

The GROUP_CONCAT() function in MySQL is used to concatenate data from multiple rows into one field. This is an aggregate (GROUP BY) function which returns a String value, if the group contains at least one non-NULL value. Otherwise, it returns NULL.

Can we use Group_concat in SQL Server?

The SQL Server Equivalent to GROUP_CONCAT() This function allows you to return a result set as a comma-separated list, as opposed to listing each row as a separate row (as with a normal result set).


3 Answers

Probably you have exceeded GROUP_CONCAT maximum length.

The result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. The value can be set higher, although the effective maximum length of the return value is constrained by the value of max_allowed_packet. The syntax to change the value of group_concat_max_len at runtime is as follows, where val is an unsigned integer:

SET [GLOBAL | SESSION] group_concat_max_len = val;
like image 160
McNets Avatar answered Nov 09 '22 01:11

McNets


Please try below code.

SET GLOBAL group_concat_max_len=15000;
SELECT GROUP_CONCAT(`aid`) As favoriti 
FROM `z_web_favoriti` 
WHERE `kup_id`='1' AND `pos_id`='571'

Hope this will helps.

like image 20
Sagar Gangwal Avatar answered Nov 09 '22 01:11

Sagar Gangwal


By default, the maximum length for group_concat() is 1,024.

You can change this to a larger value by changing the value of the system variable group_concat_max_len.

The documentation explains this.

like image 1
Gordon Linoff Avatar answered Nov 09 '22 01:11

Gordon Linoff