在 MySQL 中将行转换为列

2022-01-22 00:00:00 pivot mysql

如何在 MySQL 查询中将行转换为列?

How can I convert rows into columns in a MySQL query?

推荐答案

你可以用 GROUP_CONCAT 将行变成 a 列,但是你不能转置整个结果集以任何自动方式.您可以编写一个手动生成每一列的查询,也可以在应用程序中进行.

You can turn rows into a column with GROUP_CONCAT, but you can't transpose whole result sets in any automatic way. You either write a query that produces each column manually, or you do it in an application.

这里有一个关于编写复杂查询来模拟转置的教程:

Here's a tutorial on writing the complicated queries to emulate the transposition:

http://www.artfulsoftware.com/infotree/queries.php#78

相关文章