我有一个名为 dir
的数据库表,其中包含如下行:
field1 field2 filed 3
1 name sam
1 age 18
1 class 12
2 name jolly
2 age 14
2 class 8
. . .
. . .
. . .
我想使用我的 SQL 查询将其显示如下
no name age class
1 sam 18 12
2 jolly 14 8
最佳答案
您可以使用条件聚合:
select field1,
max(case when field2 = 'name' then field3 end) as name,
max(case when field2 = 'age' then field3 end) as age,
max(case when field2 = 'class' then field3 end) as class
from t
group by field1;
关于mysql - 进行 SQL 查询以给出如下所示的特定结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57711068/