php - MySQL multiple selects in one query -


for example have few tables:
products:

| product_id | name   | price | | 1          | apple  | 20.32 | | 2          | pear   | 9.99  | | 3          | banana | 1.5   | 

product attribute:

| attr_id | name   | value | | 1       | weight | 10 kg | | 2       | date   | 2013  | | 3       | color  | red   | 

...and on.
product-attribute relations table:

| product_id | attr_id | | 1          | 3       | | 2          | 1       | | 1          | 2       | | 3          | 2       | 

my question : there available construct 1 select request query returns product 1 , 2 in following data structure(or similar)? should run deveral select requests first "where product_id in (1, 2)" , throught loop select them attributes.
sorry bad english :]

array(     [0] = array(           product_id = 1,           name = apple,           attributes= array(                         [0] => array(                            attr_id = 3,                            name = color,                            value = red,                         ),                         [0] => array(                            attr_id = 2,                            name = date,                            value = 2013,                         )                         ),     ),     [1] = array(           product_id = 2,           name = apple,           attributes= array(                         [0] => array(                            attr_id = 1,                            name = veight,                            value = 10 kg,                         ),                       ),     )   ) 

this not matter of query, php code. fit:

$rselect = mysqli_query('select      products.id record_id,      products.name products_name,      products.price product_price,       attributes.id attribute_id,      attributes.name attribute_name,      attributes.value attribute_value         products       left join products_attributes        on products.id=products_attributes.product_id      left join attributes        on products_attributes.attr_id=attributes.id', $rconnect);  $rgresult = []; while($rgrow = mysqli_fetch_array($rselect)) {    $rgresult[$rgrow['record_id']]['product_id']   = $rgrow['record_id'];    $rgresult[$rgrow['record_id']]['name']         = $rgrow['product_name'];    $rgresult[$rgrow['record_id']]['price']        = $rgrow['product_price'];    $rgresult[$rgrow['record_id']]['attributes'][] = [       'attr_id' => $rgrow['attribute_id'],       'name'    => $rgrow['attribute_name'],       'value'   => $rgrow['attribute_value'],    ]; }; //var_dump($rgresult); 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -