1. 利用max(case when then)

聚合函数,max,取最大值

 (case  when course = '语文' then score else 0 end) ---判断

as 语文---别名作为列名

MySQL列变成行 利用max(case when then)

 

 2.max(when course='语文' then score else 0 end)

mysql> SELECT
    ->  `name`,
    ->  MAX(
    ->   CASE 
    ->   WHEN course='语文' THEN
    ->    score else 0
    ->   END
    ->  ) AS 语文,
    ->  MAX(
    ->   CASE 
    ->   WHEN course='数学' THEN
    ->    score else 0
    ->   END
    ->  ) AS 数学, 
    ->  MAX(
    ->   CASE 
    ->   WHEN course='英语' THEN
    ->    score else 0
    ->   END
    ->  ) AS 英语
    -> FROM
    ->  student
    -> GROUP BY `name`
    -> ;

MySQL列变成行 利用max(case when then)

 参考:https://www.jb51.net/article/109487.htm

相关文章:

  • 2021-06-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-11
  • 2022-02-24
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-27
  • 2021-08-24
  • 2021-07-20
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
相关资源
相似解决方案