Home > Database > Mysql Tutorial > body text

Oracle中行转列以及Join小总结

WBOY
Release: 2016-06-07 17:06:07
Original
1117 people have browsed it

在Oracle中行转列,可以利用decode函数: 如有学生表A: ID NAME SUBJECT SCORE 1 张三 语文 90 2 张三 数学 80 3 李四

在Oracle中行转列,,可以利用decode函数:

如有学生表A:

ID NAME SUBJECT SCORE

1 张三 语文 90

2 张三 数学 80

3 李四 语文 99

4 李四 数学 78

5 张三 英语 89


现要转换成下表:

NAME 语文 数学 英语

张三 90 80 89

李四 99 78  


这是一个典型的行转列过程,只需如下SQL即可:

select NAME, sum(decode(SUBJECT,'语文', SCORE, null)), sum(decode(SUBJECT,'数学', SCORE, null)), sum(decode(SUBJECT,'英语', SCORE, null)) from A  group by NAME;


关于Join:

1.inner join :

select * from A a, B b where a.id = b.aId 与

select * from A a inner join B b on a.id = b.aId 是一样的;

2. left join 与 left outer join:

select * from A a left join B b on (a.id = b.aId);

此时,不管B中有没有对应A的记录,都会查出A表中的所有记录;

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!