mybatis 动态行转列

 

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>
posted @ 2018-12-05 14:38  沫小淘  阅读(6321)  评论(0编辑  收藏  举报