Home  >  Article  >  Database  >  常用的一些SQL语句整理,也许有你想要的。

常用的一些SQL语句整理,也许有你想要的。

WBOY
WBOYOriginal
2016-06-07 17:40:27988browse

本篇文章是对一些常用的sql语句进行了总结与分析,需要的朋友参考下,也许会有你需要的。 1.SQL行列转换 问题:假设有张学生成绩表(tb)如下: 姓名 课程 分数 张三 语文 74 张三 数学 83 张三 物理 93 李四 语文 74 李四 数学 84 李四 物理 94 想变成(得到如

本篇文章是对一些常用的sql语句进行了总结与分析,需要的朋友参考下,香港服务器,也许会有你需要的。

1.SQL行列转换

问题:假设有张学生成绩表(tb)如下:
姓名 课程 分数
张三 语文 74
张三 数学 83
张三 物理 93
李四 语文 74
李四 数学 84
李四 物理 94
想变成(得到如下结果): 
姓名 语文 数学 物理 
---- ---- ---- ----
李四 74 84 94
张三 74 83 93

sql语句为:

select 姓名 as 姓名 , max(case 课程 when '语文' then 分数 else 0 end) 语文, max(case 课程 when '数学' then 分数 else 0 end) 数学, max(case 课程 when '物理' then 分数 else 0 end) 物理 from tb group by 姓名 ,香港服务器,虚拟主机

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn