- Previous thread: [PHP] Does anyone know how gettext works under the hood?
- Next thread: [PHP] Removing empty values from array
- Threads sorted by date: php 200906
Ummm yeah, its going to take me a while to wrap my head around that one.
thanks though!
Matt
On Thu, Jun 25, 2009 at 5:07 PM, Andrew Ballard wrote:
> On Thu, Jun 25, 2009 at 4:21 PM, Andrew Ballard wrote:
> > On Thu, Jun 25, 2009 at 3:20 PM, Matt Giddings wrote:
> >> I know this is the off topic (sorry), but it is a php project that I'm
> >> working on! I need some pointers on how to pivot a mysql column
> (containing
> >> comma delimited data) into an equal number of rows (see example). Any
> >> direction (pointers to links, etc. would be appreciated).
> >>
> >> From this:
> >>
> >> user.table
> >> uid|name|groups
> >> 1|mcgiddin|1,4,7,10,12
> >>
> >>
> >> To this:
> >>
> >> pivot.table
> >> uid|group
> >> 1|1
> >> 1|4
> >> 1|7
> >> 1|10
> >> 1|12
> >>
> >
> > The best performance I've seen for a SQL solution uses a tally table.
> >
> > CREATE TABLE Tally (
> > N int not null,
> > PRIMARY KEY (N)
> > )
> >
> > The table holds a sequence of numbers from 1 to some large number.
> >
> > Then you can write a query something like this:
> >
> > SELECT uid,
> SUBSTRING(user.table.groups,N+1,LOCATE(',',user.table.groups,N+1)-N-1)
> > FROM Tally, user.table
> > WHERE N < LENGTH(user.table.groups)
> > AND SUBSTRING(user.table.groups,N,1) = ','
> >
> >
> >
> >
> > Andrew
> >
>
> OK, I actually tested this and it appears I missed something. For this
> query to work, the string has to begin and end with the delimiter. So
> I just replaced user.table.groups with CONCAT(',', user.table.groups,
> ',').
>
> SELECT uid, SUBSTRING(CONCAT(',', user.table.groups,
> ','),N+1,LOCATE(',',CONCAT(',', user.table.groups, ','),N+1)-N-1)
> FROM Tally, sample
> WHERE N < LENGTH(CONCAT(',', user.table.groups, ','))
> AND SUBSTRING(CONCAT(',', user.table.groups, ','),N,1) = ','
>
>
> For more in-depth information, check out some of the links in this search:
> http://www.google.com/search?q=sql+split+tally+table
>
>
> Andrew
>
--
Matt Giddings
Web Programmer
Information Technology Services
Saginaw Valley State University
Phone: 989.964.7247
http://www.svsu.edu
thanks though!
Matt
On Thu, Jun 25, 2009 at 5:07 PM, Andrew Ballard wrote:
> On Thu, Jun 25, 2009 at 4:21 PM, Andrew Ballard wrote:
> > On Thu, Jun 25, 2009 at 3:20 PM, Matt Giddings wrote:
> >> I know this is the off topic (sorry), but it is a php project that I'm
> >> working on! I need some pointers on how to pivot a mysql column
> (containing
> >> comma delimited data) into an equal number of rows (see example). Any
> >> direction (pointers to links, etc. would be appreciated).
> >>
> >> From this:
> >>
> >> user.table
> >> uid|name|groups
> >> 1|mcgiddin|1,4,7,10,12
> >>
> >>
> >> To this:
> >>
> >> pivot.table
> >> uid|group
> >> 1|1
> >> 1|4
> >> 1|7
> >> 1|10
> >> 1|12
> >>
> >
> > The best performance I've seen for a SQL solution uses a tally table.
> >
> > CREATE TABLE Tally (
> > N int not null,
> > PRIMARY KEY (N)
> > )
> >
> > The table holds a sequence of numbers from 1 to some large number.
> >
> > Then you can write a query something like this:
> >
> > SELECT uid,
> SUBSTRING(user.table.groups,N+1,LOCATE(',',user.table.groups,N+1)-N-1)
> > FROM Tally, user.table
> > WHERE N < LENGTH(user.table.groups)
> > AND SUBSTRING(user.table.groups,N,1) = ','
> >
> >
> >
> >
> > Andrew
> >
>
> OK, I actually tested this and it appears I missed something. For this
> query to work, the string has to begin and end with the delimiter. So
> I just replaced user.table.groups with CONCAT(',', user.table.groups,
> ',').
>
> SELECT uid, SUBSTRING(CONCAT(',', user.table.groups,
> ','),N+1,LOCATE(',',CONCAT(',', user.table.groups, ','),N+1)-N-1)
> FROM Tally, sample
> WHERE N < LENGTH(CONCAT(',', user.table.groups, ','))
> AND SUBSTRING(CONCAT(',', user.table.groups, ','),N,1) = ','
>
>
> For more in-depth information, check out some of the links in this search:
> http://www.google.com/search?q=sql+split+tally+table
>
>
> Andrew
>
--
Matt Giddings
Web Programmer
Information Technology Services
Saginaw Valley State University
Phone: 989.964.7247
http://www.svsu.edu
Conversations: [PHP] OT mysql pivot table problem
- [PHP] OT mysql pivot table problem by Matt Giddings on 2009-06-25T19:31:45+00:00
- Re: [PHP] OT mysql pivot table problem by Ashley Sheridan on 2009-06-25T19:52:56+00:00
- Re: [PHP] OT mysql pivot table problem by Matt Giddings on 2009-06-25T20:12:45+00:00
- Re: [PHP] OT mysql pivot table problem by Eddie Drapkin on 2009-06-25T20:18:50+00:00
- Re: [PHP] OT mysql pivot table problem by Andrew Ballard on 2009-06-25T20:24:02+00:00
- Re: [PHP] OT mysql pivot table problem by Ashley Sheridan on 2009-06-25T20:25:29+00:00
- Re: [PHP] OT mysql pivot table problem by Andrew Ballard on 2009-06-25T21:15:52+00:00
- Re: [PHP] OT mysql pivot table problem by Matt Giddings on 2009-06-26T14:59:00+00:00
- Re: [PHP] OT mysql pivot table problem by Shawn McKenzie on 2009-06-26T15:11:07+00:00
Related Threads
- sensible-mailer - debian
- modify date in django twitter app - django
- [PATCH] kconfig: recalc symbol value before showing search results - kernel
- [Hendrix] 47 Merngpali City bueyray Nakonrachasima - firefox
- [kde-linux] Program Warning Messages - kde
- Re: MySQL execution - mysql
- slowness of syncing process - ubuntu
- [Hendrix] how can stop playing audio video large image in some site? - firefox
- [Hendrix] Firefox for Mac - firefox
- CentOS - C5 XEN domain not startable - centos