我有3个看起来像下面的表,我想将它们连接在一起,以便属性表的行成为结果表的列。
block
id name
1 a
2 b
3 c
4 d
attr_value
id value block_id attr_type_id
1 0.1 1 1
2 0.2 1 2
3 0.3 2 3
4 0.4 3 4
attr_type
id name
1 type_a
2 type_b
3 type_c
4 type_d
result table
block id name type_a type_b type_c type_d
1 a 0.1 0.2 null null
2 b null null 0.3 null
3 c null null null 0.4
4 d null null null null
我正在使用postgresql并尝试过,case when
但是这重复了一些我不想要的列(即,id = 1的多个块行)。
select b.id,b.name
,(select a.value from attr_value a inner join attr_type at on at.id=a.attr_type_id where b.id=a.block_id and at.id=1) as 'type_a'
,(select a.value from attr_value a inner join attr_type at on at.id=a.attr_type_id where b.id=a.block_id and at.id=2) as 'type_b'
,(select a.value from attr_value a inner join attr_type at on at.id=a.attr_type_id where b.id=a.block_id and at.id=3) as 'type_c'
,(select a.value from attr_value a inner join attr_type at on at.id=a.attr_type_id where b.id=a.block_id and at.id=4) as 'type_d'
from block b
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句