php - MYSQL Group By same id? -
i have following 2 tables :
table 1: user
id s_id first_name last_name ---------------------------------- 1 2 test test 2 2 hello test 3 2 hello 4 1 john smith
table 2: section
id section_name ------------------- 1 first 2 section 3 other section
based on above 2 tables , write mysql query :
select user table , group them s_id ( same s_id) , how that?
i s_id = 2 grouped array or object controlled s_id? loop through s_ids , print out first , last name?
here example of output:
$sections = array['my section'](array('first_name'=>'test' , 'last_name'=>'test'), array('first_name'=>'hello' , 'last_name'=>'test'), array('first_name'=>' now' , 'last_name'=>'hello'))
thanks
if need users s_id = 2
than:
select * `user` `s_id` = 2 # give 3 users
otherwise if group s_id
you'll 1 user each section, , think don't want that
to show all, have run query :
select user.*, section.section_name `user` inner join section on section.id = user.s_id
and trick in php:
$query = " select user.*, section.section_name `user` inner join section on section.id = user.s_id "; $result = mysqli_query($conn, $query); $final_data = array(); while($data = mysqli_fetch_assoc($result)) { $final_data[$data['section_name']][] = $data; } print($final_data); // see result
Comments
Post a Comment