我该如何转变

 SELECT
    FieldA
    FieldB
    FieldC
  FROM mydata
  ORDER BY FieldA;


+------------------------------------------+-------------------+-----------------+
| FieldA                                   | FieldB            | FieldC          |
+------------------------------------------+-------------------+-----------------+
| 123                                      | cca               |              23 |
| 123                                      | dde               |              12 |
| 234                                      | cca               |              44 |
| 234                                      | dde               |              32 |
| 456                                      | dde               |              11 |
| 456                                      | cca               |              45 |

+------------------------------------------+-------------------+-----------------+


对此:

+------------------------------------------+-------------------+-----------------+
| FieldA                                   | cca               | dde             |
+------------------------------------------+-------------------+-----------------+
| 123                                      | 23                |              12 |
| 234                                      | 44                |              32 |
| 456                                      | 32                |              11 |
+------------------------------------------+-------------------+-----------------+

最佳答案

尝试这样的事情:

    SELECT
    FieldA AS FieldA,
    SUM(IF(FieldB='cca', FieldC, 0)) AS cca,
    SUM(IF(FieldB='dde', FieldC, 0)) AS dde
    FROM mydata
    GROUP BY FieldA
    ORDER BY FieldA;

关于mysql - mysql-转换返回的数据,将字段值设置为新列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6552412/

10-11 18:00