嗨,我有两个表属性,实例
属性表
id | key_info | value_info
2 | amount | 1009
2 | currency | USD
2 | Level | Level 5
3 | amount | 2017
3 | currency | CAD
实例表
id | status
2 | Pending
3 | Approved
我想像这样连接两个表-
新表
id | amount | currrency | level | status
2 | 1001 | USD | Level 5 | Pending
3 | 2017 | CAD | | Approved
Attribute 和 Instance 中的所有字段都是可选的,除了 id。
最佳答案
select
a.id,
max(case when a.key_info = 'amount' then a.value_info end) as amount,
max(case when a.key_info = 'currency' then a.value_info end) as currency,
max(case when a.key_info = 'level' then a.value_info end) as level,
i.status
from
attribute a
join instance i on a.id = i.id
group by
a.id,
i.status
Sql Fiddle
关于sql - 将行转置为列并连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15234212/