这很痛苦,但您可以使用变量和聚合来完成:
select max(doctor) as doctor,
max(engineer) as engineer,
max(developer) as developer,
max(musician) as musician
from ((select name as doctor, null as engineer, null as developer, null as musician,
(@rnd := @rnd + 1) as rn
from t cross join
(select @rnd := 0) as params
where occupation = 'doctor'
) union all
(select null as doctor, name as engineer, null as developer, null as musician,
(@rne := @rne + 1) as rn
from t cross join
(select @rne := 0) as params
where occupation = 'engineer'
) union all
(select null as doctor, null as engineer, name as developer, null as musician,
(@rnv := @rnv + 1) as rn
from t cross join
(select @rnv := 0) as params
where occupation = 'developer'
) union all
(select null as doctor, null as engineer, null as developer, name as musician,
(@rnm := @rnm + 1) as rn
from t cross join
(select @rnm := 0) as params
where occupation = 'musician'
) union all
) o
group by rn;