SELECT user_name ,
MAX(CASE course WHEN '数学' THEN score END ) 数学,
MAX(CASE course WHEN '语文' THEN score END ) 语文,
MAX(CASE course WHEN '英语' THEN score END ) 英语
FROM test_tb_grade
GROUP BY USER_NAME;
在 mybatis 中实现 动态行转列
<select id="list" resultType="java.util.HashMap">
SELECT user_name ,
<if test="courses != null">
<foreach collection="courses " item="item" separator=",">
MAX(CASE course WHEN #{item} THEN score END ) "${item}"
</foreach>
</if>
FROM test_tb_grade
GROUP BY USER_NAME;
</select>