sql - MySQL - Combining Records into Fields -
what i'm trying query table holds custom data person , fields, i'm getting each individual field record in return.
the current statement i'm using is
select s.fname, s.lname, s.email, s.mobile, s.country, cf.name, ca.value standard s inner join people p on (s.pid = p.pid) inner join custom_answers ca on (ca.pid = p.pid) inner join custom_fields cf on (cf.fieldid = ca.fieldid) p.acctid = 'xxxxxxxxxx'
this given resultset of 22,000 rows, whereas looking 900 rows.
an example of data output is
fname | lname | email | mobile | country | name | value tom | smith | t@t | 0412 | au | state | vic tom | smith | t@t | 0412 | au | position | dept head tom | smith | t@t | 0412 | au | guest | john smith mick | jones | m@j | 0411 | au | postnom | aoc mick | jones | m@j | 0411 | au | state | nsw mick | jones | m@j | 0411 | au | postcode | 2000
whereas output is
fname | lname | email | mobile | country | state | position | guest | postnom | postcode tom | smith | t@t | 0412 | au | vic | dept head | john smith | null | null mick | jones | m@j | 0411 | au | nsw | null | null | aoc | 2000
what may or may not cause complication amount of custom fields isn't same each person. may have handful or custom fields, may have upwards of 30.
you'll have apply group by, non-aggregates, , create roll-up... applying max() based on comparing "cf.name" value each element, you'll value per "as" column... if no such record found, remain blank (i've left empty spaces, null...
select s.fname, s.lname, s.email, s.mobile, s.country, max( if( cf.name = 'state', ca.value, ' ' )) state, max( if( cf.name = 'position', ca.value, ' ' )) position, max( if( cf.name = 'guest', ca.value, ' ' )) guest, max( if( cf.name = 'postnom', ca.value, ' ' )) postnom, max( if( cf.name = 'postcode', ca.value, ' ' )) postcode standard s inner join people p on s.pid = p.pid inner join custom_answers ca on p.pid = ca.pid inner join custom_fields cf on ca.fieldid = cf1.fieldid p.acctid = 'xxxxxxxxxx' group s.fname, s.lname, s.email, s.mobile, s.country
Comments
Post a Comment