《Mysql应用数据库实现行列转换(mysql示例)》要点:
本文介绍了Mysql应用数据库实现行列转换(mysql示例),希望对您有用。如果有疑问,可以联系我们。
MYSQL教程这篇文章通过sql示例代码给大家介绍了mysql数据库如何实现行列转换,下面话不多说,直接来看示例代码吧.
MYSQL教程原表:
MYSQL教程
表名 :user
----------------------------------------
name | course | grade
----------------------------------------
zhangsan | Java | 70
----------------------------------------
zhangsan | C++ | 80
----------------------------------------
lisi | java | 90
----------------------------------------
lisi | C# | 60
----------------------------------------
MYSQL教程用一条 SQL 语句得到如下形式:
MYSQL教程
----------------------------------------
name | java | C++ | C#
----------------------------------------
zhangsan | 70 | 80 | null
----------------------------------------
lisi | 90 | null | 60
----------------------------------------
MYSQL教程方案一
MYSQL教程
select name,
sum(case when course='java' then grade end) as java,
sum(case when course='C++' then grade end) as C++,
sum(case when course='C#' then grade end) as C#
from test group by name
MYSQL教程方案二
MYSQL教程
select distinct c.`name` AS name,
(select grade from test where name = c.`name` and course = 'java' )as java,
(select grade from test where name = c.`name` and course = 'C++' )as C++,
(select grade from test where name = c.`name` and course = 'C#' )as C#
from test c
MYSQL教程总结
MYSQL教程以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能有一定的帮助,如果有疑问大家可以留言交流.
转载请注明本页网址:
http://www.vephp.com/jiaocheng/4975.html