Thursday, July 11, 2013

Comma separated list - MySQL

Today i was actually almost going to script this in Python. I got a list of over 1000 ids that i wanted to output as comma separate to resuse in my R scripts. I just found out that you can use GROUP_CONCAT to perform that.
SELECT GROUP_CONCAT(refugees.id) AS accounts
FROM refugees
Bang and that will output everything as comma separated list to be reused in other locations something like this
“123421, 3245321, 245432, 243624562, 256132, 24523, 786,4243, 2562”

Finally and most annoying thing here, is that the maximum length of the result of this function by default is 1024 characters, so know that if you have a large set then its truncated !! damn i didn't know this and was getting wrong incomplete stats.
Looking up online and now i'm very well aware that I can change the param group_concat_max_len to increase this limit:
In your MySQL shell run this : - 
SET SESSION group_concat_max_len = 1000000;
now bang, we are good to go. 

No comments:

Post a Comment

Add any comments if it helped :)