Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql group_concat with ignoring empty string column

Tags:

sql

mysql

MYSQL group_concat() function by default ignore null columns but it didn't ignore empty string columns. I have a field which is mediumtext type and not null. When I am using group_concat function on that the query generated unwanted ',,,' this type of value. How can i avoid this? Thanks in advance for your valuable time.

like image 784
karim_fci Avatar asked Nov 19 '14 06:11

karim_fci


2 Answers

Transfer empty string to null:

NULLIF(column1,'')
  • NULLIF(expr1,expr2):
    • Return NULL if expr1 = expr2
like image 124
Jaugar Chang Avatar answered Sep 28 '22 14:09

Jaugar Chang


Use GROUP_CONCAT with IF(expr1,expr2,expr3), assume that you want concat column1:

GROUP_CONCAT(IF(column1='', null, column1)) 
like image 27
anhlc Avatar answered Sep 28 '22 15:09

anhlc