mySQL: Subquery to array? -
i working on slight complex (at least me) mysql query containing subquery , isn't going honest.
select `products`.`id`, `product`.`price`, ( select `value` (`productvalues`) `productvalues`.`product` = 'product.id' ) values (`products`) where`product`.`active` = 1
the current result looks this:
array ( [0] => array ( [id] => 1 [active] => 1 [price] => 1000 [values] => ) )
what want values element become array elements in values table matches (where productvalues.product = product.id
).
what doing wrong?
select p.id, p.price, pv.`value` products p join productvalues pv on p.product_id=pv.product p.active = 1 order p.id;
gives table 1 row each pv.value (btw, using reserved words 'value' not recommended). ordering output p.id ensures rows particular product together. so, in application layer, loop through rows, changing product each time p.id changes.
$old_id=null; $datastructure=array(); while($arr=$result->fetch_assoc()){ if($arr['id']!=$old_id){ $datastructure[$arr['id']][price]=$arr['price']; $old_id=$arr['id']; } $datastructure[$arr['id']]['values'][]=$arr['value']; }
the structure i've given perhaps more flexible 1 asked in allows access particular product through array key.
Comments
Post a Comment