sql - Distinct,REGEXP apply to Field and CONCAT_GROUP in MYSQL to remove repeated words to stored procedure -
context:
i've following table(example):
| id | name | country | --------------------------------------------- | 1 | cristian | francia,holanda,alemania | | 2 | andrea | francia,espaÑa,belgica | | 3 | fabian | belgica,alemania |
i need put countries in field, need there aren't repeat values.
so, i'm trygin following query:
select group_concat(distinct(country)) usuario;
or using regular expresion :
select group_concat(distinct(country)) usuario group_concat(country) regexp 'somepattern'
the wrong answer next:
francia,holanda,alemania,francia,espaÑa,belgica,belgica,alemania
the expected answer is:
francia,holanda,alemania,espaÑa,belgica
or make stored procedure?
how expected answer, to n values , differing values?
thanks knowledge , time!.
there's no builtin function in mysql.
it's possible boatload of string processing in mysql, it's ugly, , there has known finite limit on number of string values in comma separated list.
here's 1 way distinct list of field values comma separated lists:
select substring_index(substring_index(concat(country,','),',',1),',',-1) fld mytable having fld <> '' union select substring_index(substring_index(concat(country,','),',',2),',',-1) fld mytable having fld <> '' union select substring_index(substring_index(concat(country,','),',',3),',',-1) fld mytable having fld <> '' union select substring_index(substring_index(concat(country,','),',',4),',',-1) fld mytable having fld <> '' union select substring_index(substring_index(concat(country,','),',',5),',',-1) fld mytable having fld <> '' union select substring_index(substring_index(concat(country,','),',',6),',',-1) fld mytable having fld <> '' order 1
i'll leave exercise figure out that's doing , how works.
now that's each value in separate row, we'd think might want leave way.
it's easy wrap query in query, , use group_concat function, , return single row string value containing comma separated list.
Comments
Post a Comment