php - Join Three tables in mysql with weird requirement -
i have 3 tables in db.
table a
has fields
keyid | keyname 27 | income 28 | account number
table b
has fields
userid | email | name | phone 481 | test@gmail.com | test | 99999999
table c
has fields
id | keyid | userid | value 1 | 27 | 481 | 10,000
i need display table fields headers are:
userid | email | name | phone | income
and table values should this:
481 | test@gmail.com | test | 99999999 | 10,000
i can keyids should displayed in table. in example keyids string '27' . tried joining , can fetch & display value in table. dont know how can show key name table header.
any idea.?
you can use pair of inner join
select b.userid, b.email , b.name, c.value income tableb b inner join tablec c on b.userid = c.userid inner join tablea on a.keyid = c.keyid , a.keyname = 'income';
and query provided in comment
select b.userid , b.email , b.name , group_concat(distinct concat(c.keyid,’^:^’,c.value) order c.id separator ‘;’) keyvalues tableb b inner join tablec c on b.userid = c.userid inner join tablea on a.keyid = c.keyid;
and case should
select b.userid , b.email , b.name , group_concat(distinct case when c.keyid in ('1,23,10') concat(c.keyid,’^:^’,c.value) end order c.id separator ‘;’) keyvalues tableb b inner join tablec c on b.userid = c.userid inner join tablea on a.keyid = c.keyid;
Comments
Post a Comment