给定两个表:
'people' 表包含以下列:
name
favorite_walking_shoe
favorite_running_shoe
favorite_dress_shoe
favorite_house_shoe
favorite_other_shoe
'shoes' 表包含以下列:
shoe
name
description
我想创建一个包含以下内容的结果集:
people.name, people.favorite_shoe_type, shoes.name, shoes.description
我知道我可以使用以下方法获得所需的结果:
select p.name, p.favorite_shoe_type, s.name, s.description
from (select name, favorite_walking_shoe as shoe, 'walking' as favorite_shoe_type
from people where favorite_walking_shoe is not null
union all
select name, favorite_running_shoe, 'running'
from people where favorite_running_shoe is not null
union all
select name, favorite_dress_shoe, 'dress'
from people where favorite_dress_shoe not is null
union all
select name, favorite_house_shoe, 'house'
from people where favorite_house_shoe not is null
union all
select name, favorite_other_shoe, 'other'
from people where favorite_other_shoe not is null
) p
join shoes s on s.shoe = p.shoe
order by 1,2
但这需要 'people' 表的 5 次通行证。有没有一种方法可以在不需要多次通过的情况下完成 UNION ALL?
我应该指出,这些结构是我无法修改的供应商产品的一部分。:(