Go Back   Forum Care Forums > Development Reference Area > MySQL Discussion

Reply
 
LinkBack Thread Tools Display Modes
exploding strings
Old
  (#1)
strawberry
Guest
 
Posts: n/a
Default exploding strings - 06-04-2007, 06:58 AM

In a moment or two I'm going to have a table that looks a bit like
this:
id hyphenated
1 [all-bar-one]
2 [black-and-white]
3 [black-and-blue]
4 [all-for-one]

>From which I'd like to generate a result set like this:


all
and
bar
black
blue
for
one
white

Is there a simple way of doing this without resorting to PHP?

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: exploding strings
Old
  (#2)
lark
Guest
 
Posts: n/a
Default Re: exploding strings - 06-04-2007, 06:58 AM

strawberry wrote:
> In a moment or two I'm going to have a table that looks a bit like
> this:
> id hyphenated
> 1 [all-bar-one]
> 2 [black-and-white]
> 3 [black-and-blue]
> 4 [all-for-one]
>
>>From which I'd like to generate a result set like this:

>
> all
> and
> bar
> black
> blue
> for
> one
> white
>
> Is there a simple way of doing this without resorting to PHP?
>

are the brackets part of the data?

--
lark -- EMAIL REMOVED
To reply to me directly, delete "despam".
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: exploding strings
Old
  (#3)
strawberry
Guest
 
Posts: n/a
Default Re: exploding strings - 06-04-2007, 06:58 AM

On 18 May, 19:20, lark <ham...@sbcdeglobalspam.net> wrote:
> strawberry wrote:
> > In a moment or two I'm going to have a table that looks a bit like
> > this:
> > id hyphenated
> > 1 [all-bar-one]
> > 2 [black-and-white]
> > 3 [black-and-blue]
> > 4 [all-for-one]

>
> >>From which I'd like to generate a result set like this:

>
> > all
> > and
> > bar
> > black
> > blue
> > for
> > one
> > white

>
> > Is there a simple way of doing this without resorting to PHP?

>
> are the brackets part of the data?
>
> --
> lark -- ham...@sbcdeglobalspam.net
> To reply to me directly, delete "despam".



Yes - but disregarding the brackets is the easy part!

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: exploding strings
Old
  (#4)
lark
Guest
 
Posts: n/a
Default Re: exploding strings - 06-04-2007, 06:58 AM

strawberry wrote:
> On 18 May, 19:20, lark <ham...@sbcdeglobalspam.net> wrote:
>> strawberry wrote:
>>> In a moment or two I'm going to have a table that looks a bit like
>>> this:
>>> id hyphenated
>>> 1 [all-bar-one]
>>> 2 [black-and-white]
>>> 3 [black-and-blue]
>>> 4 [all-for-one]
>>> >From which I'd like to generate a result set like this:
>>> all
>>> and
>>> bar
>>> black
>>> blue
>>> for
>>> one
>>> white
>>> Is there a simple way of doing this without resorting to PHP?

>> are the brackets part of the data?
>>
>> --
>> lark -- ham...@sbcdeglobalspam.net
>> To reply to me directly, delete "despam".

>
>
> Yes - but disregarding the brackets is the easy part!
>



try this:

select distinct SUBSTRING_INDEX(hyphenated,'-', 1) as col1 from t1
union
select distinct substring_index(substring(hyphenated,
instr(substring_index(hyphenated, '-', 2), '-')+1),'-',1) as col1 from t1
union
select distinct SUBSTRING_INDEX(hyphenated,'-',-1) as col1 from t1
order by col1



hyphenated is the name of column in t1 table.
--
lark -- EMAIL REMOVED
To reply to me directly, delete "despam".
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: exploding strings
Old
  (#5)
strawberry
Guest
 
Posts: n/a
Default Re: exploding strings - 06-04-2007, 06:58 AM

On May 18, 7:52 pm, lark <ham...@sbcdeglobalspam.net> wrote:
> strawberry wrote:
> > On 18 May, 19:20, lark <ham...@sbcdeglobalspam.net> wrote:
> >> strawberry wrote:
> >>> In a moment or two I'm going to have a table that looks a bit like
> >>> this:
> >>> id hyphenated
> >>> 1 [all-bar-one]
> >>> 2 [black-and-white]
> >>> 3 [black-and-blue]
> >>> 4 [all-for-one]
> >>> >From which I'd like to generate a result set like this:
> >>> all
> >>> and
> >>> bar
> >>> black
> >>> blue
> >>> for
> >>> one
> >>> white
> >>> Is there a simple way of doing this without resorting to PHP?
> >> are the brackets part of the data?

>
> >> --
> >> lark -- ham...@sbcdeglobalspam.net
> >> To reply to me directly, delete "despam".

>
> > Yes - but disregarding the brackets is the easy part!

>
> try this:
>
> select distinct SUBSTRING_INDEX(hyphenated,'-', 1) as col1 from t1
> union
> select distinct substring_index(substring(hyphenated,
> instr(substring_index(hyphenated, '-', 2), '-')+1),'-',1) as col1 from t1
> union
> select distinct SUBSTRING_INDEX(hyphenated,'-',-1) as col1 from t1
> order by col1
>
> hyphenated is the name of column in t1 table.
> --
> lark -- ham...@sbcdeglobalspam.net
> To reply to me directly, delete "despam".



Thanks. I guess something along those lines would work - certainly for
the example I provided. Unfortunately, the real world example is a
little more complicated so I was hoping for something a little more
generic, like php's explode()

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: exploding strings
Old
  (#6)
strawberry
Guest
 
Posts: n/a
Default Re: exploding strings - 06-04-2007, 06:58 AM

On May 18, 8:30 pm, strawberry <zac.ca...@gmail.com> wrote:
> On May 18, 7:52 pm, lark <ham...@sbcdeglobalspam.net> wrote:
>
>
>
> > strawberry wrote:
> > > On 18 May, 19:20, lark <ham...@sbcdeglobalspam.net> wrote:
> > >> strawberry wrote:
> > >>> In a moment or two I'm going to have a table that looks a bit like
> > >>> this:
> > >>> id hyphenated
> > >>> 1 [all-bar-one]
> > >>> 2 [black-and-white]
> > >>> 3 [black-and-blue]
> > >>> 4 [all-for-one]
> > >>> >From which I'd like to generate a result set like this:
> > >>> all
> > >>> and
> > >>> bar
> > >>> black
> > >>> blue
> > >>> for
> > >>> one
> > >>> white
> > >>> Is there a simple way of doing this without resorting to PHP?
> > >> are the brackets part of the data?

>
> > >> --
> > >> lark -- ham...@sbcdeglobalspam.net
> > >> To reply to me directly, delete "despam".

>
> > > Yes - but disregarding the brackets is the easy part!

>
> > try this:

>
> > select distinct SUBSTRING_INDEX(hyphenated,'-', 1) as col1 from t1
> > union
> > select distinct substring_index(substring(hyphenated,
> > instr(substring_index(hyphenated, '-', 2), '-')+1),'-',1) as col1 from t1
> > union
> > select distinct SUBSTRING_INDEX(hyphenated,'-',-1) as col1 from t1
> > order by col1

>
> > hyphenated is the name of column in t1 table.
> > --
> > lark -- ham...@sbcdeglobalspam.net
> > To reply to me directly, delete "despam".

>
> Thanks. I guess something along those lines would work - certainly for
> the example I provided. Unfortunately, the real world example is a
> little more complicated so I was hoping for something a little more
> generic, like php's explode()



Come to think of it, maybe this will be good enough after all. Cheers.

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top