View Single Post

  #1 (permalink)  
Old 05-05-2008
cvh@LE
 
Posts: n/a
Default nth Occurence of String in Set

Hi all,

I'm afraid I need some help with a string manipulation function in
mysql.

Suppose I have a column which contains a comma-separated set of unique
strings
e.g.

a,aab,b,bc,de
c,de,dd,dfr
xo,ff,fr

I would like to retrieve the nth element of a set in the same simple
way that find_in_set allows me retrieve the index of an element within
the list/set
e.g.
mysql> SELECT FIND_IN_SET('c','a,b,c,d');
+----------------------------+
| FIND_IN_SET('c','a,b,c,d') |
+----------------------------+
| 3 |
+----------------------------+
1 row in set (0.00 sec)

what I want is a "reverse find_in_set lookup"
like
mysql>SELECT GETELEMENT_AT_INDEX_IN_SET(3.'a,b,c,d');
....
answer would be
+------------------------------------------------------+
| GETELEMENT_AT_INDEX_IN_SET(3.'a,b,c,d');|
+-------------------------------------------------------+
| c |
+----------------------------+
1 row in set (0.00 sec)

(thats just made up)

I ' ve found away to achieve this by using various string function but
I'm quite sure there must be a better and more effective and simpler
way:

mysql> SELECT MID('a,b,c,d',
length(substring_index('a,b,c,d',",",3)),
length(substring_index('a,b,c,d',",",4))-
length(substring_index('a,b,c,d',",",3))-1
) complicated;
+-------------+
| complicated |
+-------------+
| c |
+-------------+
1 row in set (0.00 sec)

I know I could store that into a function and have my own function to
accomplish this rather nicely but isn't there a built in function to
achieve this?

Any hints or tips or reference I didnt find?

TIA

Chris



Reply With Quote