Home > Database > Mysql Tutorial > body text

SQL行列转换算法1

WBOY
Release: 2016-06-07 14:54:31
Original
1175 people have browsed it

请用一个sql语句得出结果 从table1,table2中取出如table3所列格式数据,注意提供的数据及结果不准确,只是作为一个格式向大家请教。 如使用存储过程也可以。 table1 月份mon 部门dep 业绩yj ------------------------------- 一月份 01 10 一月份 02 10 一月

请用一个sql语句得出结果
从table1,table2中取出如table3所列格式数据,注意提供的数据及结果不准确,只是作为一个格式向大家请教。
如使用存储过程也可以。

table1

月份mon 部门dep 业绩yj
-------------------------------
一月份      01      10
一月份      02      10
一月份      03      5
二月份      02      8
二月份      04      9
三月份      03      8

table2

部门dep      部门名称dname
--------------------------------
      01      国内业务一部
      02      国内业务二部
      03      国内业务三部
      04      国际业务部

table3 (result)

部门dep     一月份      二月份      三月份
--------------------------------------
      01      10           null          null
      02      10           8             null
      03      5             null          8
      04      null          9             null

------------------------------------------

select dname,b.yj '一月份',c.yj '二月份',d.yj '三月份' from table2 a left join table1 b on a.dep=b.dep and b.mon='一月份' left join table1 c on a.dep = c.dep and c.mon='二月份' left join table1 d on a.dep = d.dep and d.mon='三月份'
Copy after login
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!