php - Display output by one sql from two tables -
family table
familynumber fistname lastname 10001 b 10002 c d 10003 e f 10004 g h
phone table
familynumber phonenumber description 10001 111 mobile 10001 222 home 10001 333 text 10002 444 text 10003 555 work 10003 666 mobile
i need output as
familynumber phonenumbers 10001 111, 222, 333 10002 444 10003 555, 666
i know can done folowing code
$families = mysql_query("select * family") while($family = mysql_fetch_assoc($families)) { extract($family); echo "<td>$familynumber</td>"; echo "<td>"; $phones = mysql_query("select * phone familynumber = '$familynumber'"); while($phone = mysql_fetch_assoc($phones)) { extract($phone); echo $phonenumber . ", "; } echo "</td>"; }
but need output in 1 sql query.
using group_concat function
select p.familynumber, group_concat(distinct p.phonenumber order p.phonenumber separator ",") phonenumbers phone p left join family f on p.familynumber = f.familynumber group familynumber
Comments
Post a Comment