SELECT * FROM aaa /*数据源*/
P
PIVOT
(
SUM(id) FOR
p.bb/*需要行转列的列*/ IN ('语文','数学','英语','生物'/*列的值*/)
) AS T
你这个 PIVOT
是 SQL Server
里的,我需要的是 MySQL
@Yoooshiki: 。。。。你也可以自己写一个函数 或者 用case when:
select name,
sum(case course when '语文' then score else 0 end) as 语文,
sum(case course when '数学' then score else 0 end) as 数学,
sum(case course when '英语' then score else 0 end) as 英语
from aaaa
group by name
不会.百度:sql行转列.
面试问sql行转列怎么写.我都回:不写sql
SELECT student,
SUM(CASE course WHEN '语文'THEN score END)AS '语文',
SUM(CASE course WHEN '数学'THEN score END)AS '数学',
SUM(CASE course WHEN '英语'THEN score END)AS '英语'
FROM Score GROUP BY student
select student as name,(select b.score from score b where a.student=b.student and b.course='语文') as '语文'
,(select b.score from score b where a.student=b.student and b.course='数学') as '数学'
,(select b.score from score b where a.student=b.student and b.course='英语') as '英语' from score a group by student
前提是你的 每个人每个科目的成绩唯一