• <bdo id='wtMnM'></bdo><ul id='wtMnM'></ul>
    <tfoot id='wtMnM'></tfoot>
    <i id='wtMnM'><tr id='wtMnM'><dt id='wtMnM'><q id='wtMnM'><span id='wtMnM'><b id='wtMnM'><form id='wtMnM'><ins id='wtMnM'></ins><ul id='wtMnM'></ul><sub id='wtMnM'></sub></form><legend id='wtMnM'></legend><bdo id='wtMnM'><pre id='wtMnM'><center id='wtMnM'></center></pre></bdo></b><th id='wtMnM'></th></span></q></dt></tr></i><div id='wtMnM'><tfoot id='wtMnM'></tfoot><dl id='wtMnM'><fieldset id='wtMnM'></fieldset></dl></div>

    <legend id='wtMnM'><style id='wtMnM'><dir id='wtMnM'><q id='wtMnM'></q></dir></style></legend>

        <small id='wtMnM'></small><noframes id='wtMnM'>

        PHP/MySQL 按列分组结果

        时间:2023-10-04

          <legend id='JGLzM'><style id='JGLzM'><dir id='JGLzM'><q id='JGLzM'></q></dir></style></legend>

              <small id='JGLzM'></small><noframes id='JGLzM'>

            • <i id='JGLzM'><tr id='JGLzM'><dt id='JGLzM'><q id='JGLzM'><span id='JGLzM'><b id='JGLzM'><form id='JGLzM'><ins id='JGLzM'></ins><ul id='JGLzM'></ul><sub id='JGLzM'></sub></form><legend id='JGLzM'></legend><bdo id='JGLzM'><pre id='JGLzM'><center id='JGLzM'></center></pre></bdo></b><th id='JGLzM'></th></span></q></dt></tr></i><div id='JGLzM'><tfoot id='JGLzM'></tfoot><dl id='JGLzM'><fieldset id='JGLzM'></fieldset></dl></div>

                <bdo id='JGLzM'></bdo><ul id='JGLzM'></ul>
                  <tbody id='JGLzM'></tbody>
                1. <tfoot id='JGLzM'></tfoot>
                2. 本文介绍了PHP/MySQL 按列分组结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  为了尽可能少的保留SQL语句,我想从MySQL做select set:

                  in order to keep as few SQL statements as possible, I want to do select set from MySQL:

                  SELECT * FROM products WHERE category IN (10,120,150,500) ORDER BY category,id;
                  

                  现在,我有以下方式的产品列表:

                  Now, I have list of products in following manner:

                  CATEGORY
                   - product 1
                   - product 2
                  CATEGORY 2
                   - product 37
                  ...
                  

                  处理 MySQL 结果的最佳和最有效的方法是什么?

                  What's the best and most efficent way to process MySQL result?

                  我认为类似(伪 PHP)

                  I thought something like (pseudo PHP)

                  foreach ($product = fetch__assoc($result)){
                    $products[$category][] = $product;
                  }
                  

                  然后在输出时,做foreach循环:

                  and then when outputting it, do foreach loop:

                  foreach($categories as $category){
                    foreach($products[$category] as $product){
                      $output;
                    }
                  }
                  

                  这是最好的,还是像mysql_use_groupby之类的神奇东西?

                  Is this the best, or is something magical like mysql_use_groupby or something?

                  推荐答案

                  就像 mluebke 评论的那样,使用 GROUP 意味着您只能获得每个类别的一个结果.根据您提供的列表作为示例,我认为您想要这样的东西:

                  Like mluebke commented, using GROUP means that you only get one result for each category. Based on the list you gave as an example, I think you want something like this:

                  $sql = "SELECT * FROM products WHERE category IN (10,120,150,500) GROUP BY category ORDER BY category, id";
                  $res = mysql_query($sql);
                  
                  $list = array();
                  while ($r = mysql_fetch_object($res)) {
                    $list[$r->category][$r->id]['name'] = $r->name;
                    $list[$r->category][$r->id]['whatever'] = $r->whatever;
                    // etc
                  }

                  然后遍历数组.示例:

                  foreach ($list as $category => $products) {
                    echo '<h1>' . $category . '</h1>';
                  
                    foreach ($products as $productId => $productInfo) {
                      echo 'Product ' . $productId . ': ' . $productInfo['name'];
                      // etc
                    }
                  
                  }

                  这篇关于PHP/MySQL 按列分组结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:您如何将网站部署到您的网络服务器? 下一篇:PHP 输出显示带有问号的黑色小菱形

                  相关文章

                  最新文章

                    <bdo id='YCwRj'></bdo><ul id='YCwRj'></ul>

                  <small id='YCwRj'></small><noframes id='YCwRj'>

                    1. <legend id='YCwRj'><style id='YCwRj'><dir id='YCwRj'><q id='YCwRj'></q></dir></style></legend>

                    2. <i id='YCwRj'><tr id='YCwRj'><dt id='YCwRj'><q id='YCwRj'><span id='YCwRj'><b id='YCwRj'><form id='YCwRj'><ins id='YCwRj'></ins><ul id='YCwRj'></ul><sub id='YCwRj'></sub></form><legend id='YCwRj'></legend><bdo id='YCwRj'><pre id='YCwRj'><center id='YCwRj'></center></pre></bdo></b><th id='YCwRj'></th></span></q></dt></tr></i><div id='YCwRj'><tfoot id='YCwRj'></tfoot><dl id='YCwRj'><fieldset id='YCwRj'></fieldset></dl></div>
                      <tfoot id='YCwRj'></tfoot>