Search code examples
mysqlheidisqlcmsmadesimple

MySQL separate values by user


i have recovered a database from a cmsms website and with Mysql i want to extract all the login information of the front-end users . i work with Heidisql

My problem user_proprerties DB is in this form:

Id UserID Type    Data
1       2 email   [email protected]
2       2 company lorme ipsum
3       2 fname   testname
4       5 email&  [email protected]
5       5 company empty
6       5 fname   dolor sir amed

i want the data to be in this form:
Userid email company fname
2 [email protected] lorem testname
5 [email protected] emptydolor sir amed

this is the current mysql query that i have

select 
    cms_module_feusers_users.id,
    cms_module_feusers_users.username,  
    cms_module_feusers_users.createdate,
    cms_module_feusers_users.expires,
    cms_module_feusers_properties.`data` email
from 
    cms_module_feusers_users
    inner join cms_module_feusers_properties on cms_module_feusers_properties.userid = cms_module_feusers_users.id
where
    cms_module_feusers_properties.title = 'email'

But now i'm stuck when i want the company name and fname.


Solution

  • (Fractionally) slower, but cleaner...

    SELECT p.userid
         , MAX(CASE WHEN type = 'email' THEN data END) email
         , MAX(CASE WHEN type = 'company' THEN data END) company
         , MAX(CASE WHEN type = 'fname' THEN data END) fname
      FROM cms_module_feusers_properties p
     GROUP
        BY p.userid;