简体   繁体   English

如何按列组排列SQL查询结果?

[英]How to arrange SQL query result with column group?

SELECT Product_type FROM product INNER JOIN product Product_type.typeid = product.pid
I have this sql query result list. This is exactly what i need.

--------------------------
Product_type  | Product
--------------------------
ProductType 1 | Poduct A
ProductType 1 | Poduct B
ProductType 1 | Poduct C
ProductType 1 | Poduct D
ProductType 2 | Poduct E
ProductType 2 | Poduct F
ProductType 2 | Poduct G
--------------------------

Now, my question is how to get list of product based on product type. 现在,我的问题是如何根据产品类型获取产品列表。 Is there any way to get such result with sql query? 有什么办法可以通过sql查询得到这样的结果?

I need frontend output as bellow: 我需要前端输出如下:

ProductType 1
    | Product A
    | Product B
    | Product C
    | Product D
ProductType 2
    | Product E
    | Product F
    | Product G

Or how can i store result in multi-dimational array based on product_type. 或者如何将结果存储在基于product_type的多维数组中。 Can anyone help. 谁能帮忙。

Big Thanks an advance! 非常感谢提前!

Fetch the result and loop through it to get the desired result 获取结果并循环遍历以获得所需结果

$sql = "SELECT Product_type FROM product INNER JOIN product Product_type.typeid = product.pid";

$result = $conn->query($sql);

while ($row = $result->fetch_assoc()) {

          $newarray[$row['product_type']][] = $row['product']; 

        }




 print_r($newarray);

Try using this 试试这个

SELECT Product_type FROM product INNER JOIN product Product_type.typeid = product.pid GROUP BY Product_type;

Let me know if it works or not. 让我知道它是否有效。

Now, my question is how to get list of product based on product type. 现在,我的问题是如何根据产品类型获取产品列表。 Is there any way to get such result with sql query? 有什么办法可以通过sql查询得到这样的结果? Or how can i store result in multi-dimational array based on product_type. 或者如何将结果存储在基于product_type的多维数组中。 Can anyone help. 谁能帮忙。

$mysqli = new mysqli("localhost", "my_user", "my_password", "mydb");
$query = "put your query string here";

$output = array();

// From your sql_query result list
if ($result = $mysqli->query($query)) 
{

   /* fetch object array */
   while ($row = $result->fetch_assoc()) 
   {
         if(isset( $output[ $row['product_type'] ] ))
         {
                 $output[ $row['product_type'] ][] = $row['product'];
                 continue;
         }
         $output[ $row['product_type'] ] = array( $row['product']  );
   }
         print_r( $output ); // Here is your multi-dimational array
}

you fetch the results of this query in $row and then, 您在$row获取此查询的结果,然后,

$product_type1 = array();
$product_type2 = array();
while ($row) {
    if($row['Product_type'] == '1') {
        array_push($product_type1, $row['Product']);
    }
    else if($row['Product_type'] == '2') {
        array_push($product_type2, $row['Product']);
    }
}

and now use this arrays to display the values in front end. 现在使用此数组在前端显示值。

I got a solution from our discussion, it may help you. 我们的讨论提供了解决方案,可能会对您有所帮助。

Your query would be like below : 您的查询如下所示:

SELECT product_type.product_type, GROUP_CONCAT(products.product) AS pros FROM product_type LEFT JOIN products ON product_type.id = products.product_type_id GROUP BY product_type.id

This will give you output as below array: 这将为您提供如下数组的输出:

$result_arr = array(
                array(  'product_type'=>'ProductType 1',
                        'pros'=>'product A, product B, product C'),
                array(  'product_type'=>'ProductType 2',
                        'pros'=>'product D, product E, product F')
                );

Then you can manipulate that output as below : 然后,您可以如下操作该输出:

foreach($result_arr as $key=>$val){
    echo "<b>".$val['product_type']."</b><br/>";
    $products = explode(',',$val['pros']);
    foreach($products as $product){
        echo $product."</br>";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM