这是我的数据库表。我想将水平键值检索为垂直。
city_id cat_id value
100 1 1000
100 2 2000
101 1 350
101 2 500
102 1 1000
102 2 2000
103 1 10
104 2 5
输出:
city_id catagory1 catagory2
100 1000 2000
101 350 500
102 1000 2000
103 10 0
104 0 5
最佳答案
您需要一个数据透视结果:
select
city_id,
max(case cat_id when 1 then value else 0 end) as category_1,
max(case cat_id when 2 then value else 0 end) as category_2
from
my_table
group by
city_id
关于php - 从数据库中检索数据作为真实数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22245128/