我需要显示存储在MySQL数据库表中的数据摘要
control_point_run
run_date control_point duration
10/10/2016 A 100
10/10/2016 B 200
10/10/2016 C 150
10/11/2016 A 160
10/11/2016 B 220
10/11/2016 C 180
10/12/2016 A 200
10/12/2016 B 120
10/12/2016 C 180
预期产出
run_date A B C
10/10/2016 100 200 150
10/11/2016 160 220 180
10/12/2016 200 120 180
Requirement image 请帮我MySQL查询以获得预期的结果。我不知道如何将行数据转换为列标题[A,B,C]。
请注意,需要根据可用数据自动生成预期的列名(A,B,C)。该表包含的数据多于此示例中的数据,并且以后还可以插入更多的control_point值。
答案 0 :(得分:2)
使用透视查询:
SELECT run_date,
MAX(CASE WHEN control_point = 'A' THEN duration END) AS A,
MAX(CASE WHEN control_point = 'B' THEN duration END) AS B,
MAX(CASE WHEN control_point = 'C' THEN duration END) AS C
FROM yourTable
GROUP BY run_date
答案 1 :(得分:0)
这可能是对的。 请检查一下:
SELECT run_date,
(SELECT duration from tablename where control_point = 'A') as 'A',
(SELECT duration from tablename where control_point = 'B') as 'B',
(SELECT duration from tablename where control_point = 'C') as 'C'
FROM tablename
GROUP BY run_date;