开发者

group query into subarrays [duplicate]

This question already has answers here: How to multiple query results in order to reduce the query number? (4 answers) Closed 2 years ago.

hello having 2 tables like this

TABLE products_ids

id_product_unique
1
2


TABLE products_list
id_product     id_product_unique  lang   price
5              2                  en     1.00
65             2                  es     44.15
51             1                  en     53.5
9              1                  es     6.20

would it possible to create a result IN MYSQL ONLY that would create for any id_product_unique an array each one having 2 sub-arrays for "en" lang and "es" lang or I have to process the data with php anyway to get results so personalized?

example

[0]{
  id_product_unique -> 1

       [0]{
            [id_product]-> 5
            [price] -> 1.00
            [lang] -> en
         }

       [1]{

            [id_product]-> 65
            [price] -> 44.15
            [lang] -> es
        }

  }
[1]
  开发者_运维百科[id_product_unique]-> 2

// and so on


No, not in MySQL only. MySQL deals with simple rows & columns, and doesn't even know what PHP arrays are. Just loop through it once.


in php you can use

<?php

$reformated = null;
foreach ($raw as $key => $value) {
    $reformated[$value['id_product_unique']] = array_merge(
    (array)$reformated[$value['id_product_unique']],
    array(array('price'=>$value['price'], 'lag'=>$value['lang'], 'id_product'=>$value['id_product']))
    );
}
print_r($reformated)."\n\n";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜