select z.id, 姓名=xinyu,z.语文,z.数学,z.几何 from bb inner join
(
select id,数学=max(case when name='数学' then scores else 0 end)
,语文=sum(case when name='语文' then scores else 0 end)
,几何=sum(case when name='几何' then scores else 0 end)
from aa
group by id ) z
on bb.id=z.id where bb.id='1'
(
select id,数学=max(case when name='数学' then scores else 0 end)
,语文=sum(case when name='语文' then scores else 0 end)
,几何=sum(case when name='几何' then scores else 0 end)
from aa
group by id ) z
on bb.id=z.id where bb.id='1'