面试老是会考到这样一题:
将下面表中的语文,英语,数学作为行,分数总和作为结果,人名作为列查询展示出来
表结构:
即转换为这样的表结构:
转换语句:
SELECT
t.name ,
SUM(
IF (
t.subject = '语文',
t.results,
0
)
) AS '语文',
SUM(
IF (
t.subject = '数学',
t.results,
0
)
) as '数学',
SUM(
IF (
t.subject = '英语',
t.results,
0
)
) as '英语'
FROM
t_subject t
GROUP BY (t.name);