简体   繁体   English

MySQL查询将行动态转换为列和总和

[英]Mysql query to dynamically convert rows to columns and sum

Can MySQL convert columns into rows, dynamically adding as many columns as are needed for the rows. MySQL是否可以将列转换为行,从而动态添加行所需的列数。 I have tried other solutions but they dont seem to solve my unique case. 我尝试了其他解决方案,但它们似乎无法解决我的独特情况。 This is the table with the data 这是带有数据的表

Table A 表A

    +---------+-------------+------+
    |driver_id|stage_name   |points|
    +---------+-------------+------+
    |1        |Stage 1 50cc |20    |
    +---------+-------------+------+
    |1        |Stage 2 50cc |17    |
    +---------+-------------+------+
    |2        |Stage 1 50cc |17    |
    +---------+-------------+------+
    |2        |Stage 2 50cc |15    |
    +---------+-------------+------+
    |3        |Stage 1 75cc |17    |
    +---------+-------------+------+
    |3        |Stage 2 75cc |15    |
    +---------+-------------+------+
    |4        |Stage 1 75cc |15    |
    +---------+-------------+------+
    |4        |Stage 2 75cc |20    |
    +---------+-------------+------+

I would like query that formats it as follows 我想查询格式化如下

    +---------+-------------+-------------+-------------+-------------+--------------+
    |driver_id|Stage 1 50cc |Stage 2 50cc |Stage 1 75cc |Stage 2 75cc | Total Points |
    +---------+-------------+-------------+-------------+-------------+--------------+
    |1        |     20      |     17      |    null     |    null     |    37        |
    +---------+-------------+-------------+-------------+-------------+--------------+
    |2        |     17      |     15      |    null     |    null     |    32        |
    +---------+-------------+-------------+-------------+-------------+--------------+
    |3        |     null    |     null    |     16      |     15      |    31        |
    +---------+-------------+-------------+-------------+-------------+--------------+
    |4        |     null    |     null    |     15      |    20       |    35        |
    +---------+-------------+-------------+-------------+-------------+--------------+

As you can see the last col has a calculation based on the total of the various stages across for that driver. 如您所见,最后一个列基于该驱动程序各个阶段的总和进行了计算。 The sum isnt very important right now but having the scores first but if all is possible then splendid. 现在,总和并不很重要,但是首先拥有分数,但如果一切皆有可能,那么出色。

I would store the data this way (or possibly a step further in normalisation): 我将以这种方式存储数据(或者可能在规范化中更进一步):

+-----------+----------+----+--------+
| driver_id | stage_no | cc | points |
+-----------+----------+----+--------+
|         1 |        1 | 50 |     20 |
|         1 |        2 | 50 |     17 |
|         2 |        1 | 50 |     17 |
|         2 |        2 | 50 |     15 |
|         3 |        1 | 75 |     17 |
|         3 |        2 | 75 |     15 |
|         4 |        1 | 75 |     15 |
|         4 |        2 | 75 |     20 |
+-----------+----------+----+--------+

Then I would use this query: 然后,我将使用以下查询:

SELECT driver_id, stage_no, cc, points FROM my_table ORDER BY driver_id,stage,cc;

Then everything else I would handle in application code. 然后,我将在应用程序代码中处理的所有其他内容。

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

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