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

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

      <legend id='ZoPAY'><style id='ZoPAY'><dir id='ZoPAY'><q id='ZoPAY'></q></dir></style></legend>
    1. <i id='ZoPAY'><tr id='ZoPAY'><dt id='ZoPAY'><q id='ZoPAY'><span id='ZoPAY'><b id='ZoPAY'><form id='ZoPAY'><ins id='ZoPAY'></ins><ul id='ZoPAY'></ul><sub id='ZoPAY'></sub></form><legend id='ZoPAY'></legend><bdo id='ZoPAY'><pre id='ZoPAY'><center id='ZoPAY'></center></pre></bdo></b><th id='ZoPAY'></th></span></q></dt></tr></i><div id='ZoPAY'><tfoot id='ZoPAY'></tfoot><dl id='ZoPAY'><fieldset id='ZoPAY'></fieldset></dl></div>
      <tfoot id='ZoPAY'></tfoot>
    2. MySQL动态交叉表

      MySQL dynamic cross tab(MySQL动态交叉表)
        1. <i id='3W2Wr'><tr id='3W2Wr'><dt id='3W2Wr'><q id='3W2Wr'><span id='3W2Wr'><b id='3W2Wr'><form id='3W2Wr'><ins id='3W2Wr'></ins><ul id='3W2Wr'></ul><sub id='3W2Wr'></sub></form><legend id='3W2Wr'></legend><bdo id='3W2Wr'><pre id='3W2Wr'><center id='3W2Wr'></center></pre></bdo></b><th id='3W2Wr'></th></span></q></dt></tr></i><div id='3W2Wr'><tfoot id='3W2Wr'></tfoot><dl id='3W2Wr'><fieldset id='3W2Wr'></fieldset></dl></div>
            <tbody id='3W2Wr'></tbody>
          <tfoot id='3W2Wr'></tfoot>

              <bdo id='3W2Wr'></bdo><ul id='3W2Wr'></ul>

              1. <small id='3W2Wr'></small><noframes id='3W2Wr'>

                <legend id='3W2Wr'><style id='3W2Wr'><dir id='3W2Wr'><q id='3W2Wr'></q></dir></style></legend>
                本文介绍了MySQL动态交叉表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一张这样的桌子:

                way     stop    time
                1       1       00:55
                1       2       01:01
                1       3       01:07
                2       2       01:41
                2       3       01:47
                2       5       01:49
                3       1       04:00
                3       2       04:06
                3       3       04:12
                

                我想要一张这样的桌子:

                and I want a table like this:

                stop    way_1   way_2   way_3   (way_n)
                1       00:55           04:00
                2       01:01   01:41   04:06
                3       01:07   01:47   04:12
                5               01:49
                

                网上有很多关于MySQL交叉表(数据透视表)的解决方案,但是如果我不知道有多少方式",我该怎么做呢?谢谢

                There are many solutions online about MySQL cross tab (pivot table), but how can I do this if I don't know how many "way" are there? Thanks

                推荐答案

                列的数量和名称必须在您准备查询时固定.这就是 SQL 的工作方式.

                The number and names of columns must be fixed at the time you prepare the query. That's just the way SQL works.

                因此,您有两种解决方法的选择.两种选择都涉及编写应用程序代码:

                So you have two choices of how to solve this. Both choices involve writing application code:

                (1) 查询 way 的不同值,然后编写代码以使用这些值来构建数据透视查询,在 SELECT 列表中添加与不同值的数量.

                (1) Query the distinct values of way and then write code to use these to construct the pivot query, appending as many columns in the SELECT-list as the number of distinct values.

                foreach ($pdo->query("SELECT DISTINCT `way` FROM `MyTable`") as $row) {
                  $way = (int) $row["way"];
                  $way_array[] = "MAX(IF(`way`=$way, `time`)) AS way_$way";
                }
                $pivotsql = "SELECT stop, " . join(", ", $way_array) .
                   "FROM `MyTable` GROUP BY `stop`";
                

                现在您可以运行新查询,它的列数与不同 way 值的数量一样多.

                Now you can run the new query, and it has as many columns as the number of distinct way values.

                $pivotstmt = $pdo->query($pivotsql);
                

                (2) 逐行查询数据,因为它在您的数据库中是结构化的,然后在显示数据之前编写代码将其转入列.

                (2) Query the data row by row as it is structured in your database, and then write code to pivot into columns before you display the data.

                $stoparray = array();
                foreach ($pdo->query("SELECT * FROM `MyTable`") as $row) {
                  $stopkey = $row["stop"];
                  if (!array_key_exists($stopkey, $stoparray)) {
                    $stoparray[$stopkey] = array("stop"=>$stopkey);
                  }
                  $waykey = "way_" . $row["way"];
                  $stoparray[$stopkey][$waykey] = $row["time"];
                }
                

                现在你有一个数组数组,看起来和你运行一个数据透视查询一样,但你运行的实际 SQL 简单得多.您将查询结果后处理到一组不同的数组中.

                Now you have an array of arrays that looks the same as if you had run a pivot query, but the actual SQL you ran was a lot simpler. You post-processed the query result into a different set of arrays.

                这篇关于MySQL动态交叉表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
                What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
                MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)

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

                      <tbody id='Xn1Xp'></tbody>

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

                    • <tfoot id='Xn1Xp'></tfoot>

                      • <bdo id='Xn1Xp'></bdo><ul id='Xn1Xp'></ul>
                        <legend id='Xn1Xp'><style id='Xn1Xp'><dir id='Xn1Xp'><q id='Xn1Xp'></q></dir></style></legend>