gpt4 book ai didi

mysql - 重新整理返回的数据,将字段值设置为新列

转载 作者:行者123 更新时间:2023-11-29 06:15:54 24 4
gpt4 key购买 nike

我该如何改变这个

 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 - 重新整理返回的数据,将字段值设置为新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6552412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com