List:General Discussion« Previous MessageNext Message »
From:Wesley Furgiuele Date:July 1 2004 1:20am
Subject:Re: return substrings out of a string
View as plain text  

The easiest thing would be if your field always contained the same  
number of comma-separated strings. Then you could just do something  
like a SUBSTRING_INDEX() function to break out the field into separate  
values. But, assuming your field does not always contain the same  
number of strings...

I'm imagining the contents of <field1> look like "abc,def,ghi". You  
want to do:
and get back
Row1: abc
Row2: def
Row3: ghi

If that's right, off the top of my head you have two options. The first  
option I can think of would be to break out these values into another  
table. Instead of a comma-separated list inside a field, move that  
field to another table and break each value into it's own record,  
linked back to the parent table by ID. A quick search of the list  
archives will bring up better descriptions on how to normalize than I  
could give.

The other, possibly simpler, option is to handle it in PHP. You could  
just grab the field, containing a list of strings delimited by commas,  
and use the explode() function to get your results. The PHP site has  
plenty of examples on the usage of explode.


On Jun 30, 2004, at 5:35 PM, L a n a wrote:

> Hello,
> I’m working on a search function using PHP4 and MYSQL4.
> I’m looking for a function that allow to take a string value  
> (consisted of several substrings separated by coma) out of  db field  
> and return each substring one by one.
> Could you please tell me if there is a function that can do something  
> similar to
> SELECT function_name (field_name, “,”)
> (return)-> substr1, substr2, substr3
> Thank you for your help,
> Lana
> _________________________________________________________________
> Add photos to your messages with MSN Premium. Get 2 months FREE*   
> prem&xAPID=1994&DI=1034&SU= 
> enca&HL=Market_MSNIS_Taglines

return substrings out of a stringL a n a30 Jun
  • Re: return substrings out of a stringWesley Furgiuele1 Jul