我有这样的表回顾。此表用于在我的组织中回顾数据教育。 我使用的是mysql 5
mysql> select * from rekap_teams.tblrekappendidikan_divisitahunan;
+----+------------------------------------+-------+----+-----+-----+----+----+----+-----+----+----+-------+
| id | divisi | year | SD | SMP | SMA | D1 | D2 | D3 | S1 | S2 | S3 | total |
+----+------------------------------------+-------+----+-----+-----+----+----+----+-----+----+----+-------+
| 1 | CORPORATE | 2014 | 0 | 0 | 0 | 0 | 0 | 0 | 25 | 4 | 0 | 29 |
| 2 | CORPORATE SERVICES | 2014 | 0 | 0 | 137 | 0 | 1 | 2 | 195 | 4 | 1 | 340 |
| 3 | FINANCE | 2014 | 0 | 0 | 0 | 0 | 1 | 2 | 54 | 2 | 0 | 59 |
| 4 | SALES 1 | 2014 | 0 | 0 | 0 | 0 | 0 | 0 | 19 | 0 | 0 | 19 |
| 5 | SALES 2 | 2014 | 0 | 0 | 1 | 0 | 0 | 0 | 9 | 2 | 0 | 12 |
| 6 | PROMOTION | 2014 | 0 | 0 | 0 | 0 | 1 | 0 | 25 | 1 | 0 | 27 |
| 7 | NON DIVISION - SALES & MARKETING | 2014 | 0 | 0 | 0 | 0 | 0 | 0 | 66 | 1 | 0 | 67 |
| 8 | NON DIVISION - BOARD OF MANAGEMENT | 2014 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 8 | 0 | 9 |
| 9 | PROGRAMMING | 2014 | 0 | 0 | 0 | 0 | 1 | 0 | 28 | 3 | 0 | 32 |
| 10 | OPERATIONAL BROADCAST | 2014 | 0 | 0 | 4 | 0 | 9 | 9 | 376 | 3 | 0 | 401 |
| 11 | NON DIVISION | 2014 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 12 | PRODUCTION 1 | 2014 | 0 | 0 | 1 | 0 | 1 | 2 | 124 | 1 | 0 | 129 |
| 13 | PRODUCTION 2 | 2014 | 0 | 0 | 1 | 0 | 5 | 3 | 109 | 1 | 0 | 119 |
| 14 | FILM, DRAMA & SPORT | 2014 | 0 | 0 | 0 | 0 | 1 | 4 | 76 | 1 | 0 | 82 |
| 15 | NEWS | 2014 | 0 | 0 | 0 | 0 | 4 | 5 | 218 | 5 | 0 | 232 |
| 16 | PRODUCTION FACILITIES | 2014 | 0 | 0 | 2 | 0 | 4 | 13 | 597 | 5 | 0 | 621 |
| 17 | PRODUCTION 4 | 2014 | 0 | 0 | 0 | 0 | 0 | 2 | 40 | 0 | 0 | 42 |
| 18 | PCDC | 2014 | 0 | 0 | 0 | 0 | 0 | 0 | 5 | 0 | 0 | 5 |
| 19 | CORPORATE | 2015 | 0 | 0 | 0 | 0 | 0 | 0 | 26 | 3 | 0 | 29 |
| 20 | CORPORATE SERVICES | 2015 | 0 | 0 | 130 | 0 | 1 | 1 | 194 | 2 | 1 | 329 |
| 21 | FINANCE | 2015 | 0 | 0 | 0 | 0 | 1 | 1 | 49 | 2 | 0 | 53 |
| 22 | SALES 1 | 2015 | 0 | 0 | 0 | 0 | 0 | 0 | 18 | 0 | 0 | 18 |
| 23 | SALES 2 | 2015 | 0 | 0 | 1 | 0 | 0 | 0 | 9 | 2 | 0 | 12 |
| 24 | PROMOTION | 2015 | 0 | 0 | 0 | 0 | 1 | 0 | 25 | 1 | 0 | 27 |
| 25 | NON DIVISION - SALES & MARKETING | 2015 | 0 | 0 | 0 | 0 | 0 | 0 | 64 | 1 | 0 | 65 |
| 26 | NON DIVISION - BOARD OF MANAGEMENT | 2015 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 8 | 0 | 9 |
| 27 | PROGRAMMING | 2015 | 0 | 0 | 0 | 0 | 1 | 0 | 27 | 3 | 0 | 31 |
| 28 | OPERATIONAL BROADCAST | 2015 | 0 | 0 | 3 | 0 | 9 | 7 | 369 | 2 | 0 | 390 |
| 29 | NON DIVISION | 2015 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 30 | PRODUCTION 1 | 2015 | 0 | 0 | 1 | 0 | 1 | 2 | 117 | 1 | 0 | 122 |
| 31 | PRODUCTION 2 | 2015 | 0 | 0 | 1 | 0 | 5 | 2 | 106 | 1 | 0 | 115 |
| 32 | FILM, DRAMA & SPORT | 2015 | 0 | 0 | 0 | 0 | 1 | 4 | 72 | 1 | 0 | 78 |
| 33 | NEWS | 2015 | 0 | 0 | 0 | 0 | 4 | 5 | 211 | 5 | 0 | 225 |
| 34 | PRODUCTION FACILITIES | 2015 | 0 | 0 | 2 | 0 | 2 | 6 | 583 | 5 | 0 | 598 |
| 35 | PRODUCTION 4 | 2015 | 0 | 0 | 0 | 0 | 0 | 0 | 36 | 0 | 0 | 36 |
| 36 | PCDC | 2015 | 0 | 0 | 0 | 0 | 0 | 0 | 4 | 0 | 0 | 4 |
+----+------------------------------------+-------+----+-----+-----+----+----+----+-----+----+----+-------+
36 rows in set (0.00 sec)
我尝试用分组查询来解决我的问题。但我堆叠,查询如何创建这样的视图?
+------------------------------------+----+-----+-----+----+----+----+-----+----+----+----+-----+-----+----+----+----+-----+----+----+
| DIVISI | 2014 | 2015 |
+ +----+-----+-----+----+----+----+-----+----+----+----+-----+-----+----+----+----+-----+----+----+
| | SD | SMP | SMA | D1 | D2 | D3 | S1 | S2 | S3 | SD | SMP | SMA | D1 | D2 | D3 | S1 | S2 | S3 |
+------------------------------------+----+-----+-----+----+----+----+-----+----+----+----+-----+-----+----+----+----+-----+----+----+
| CORPORATE | 0 | 0 | 0 | 0 | 0 | 0 | 25 | 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 25 | 4 | 0 |
| CORPORATE SERVICES | 0 | 0 | 137 | 0 | 1 | 2 | 195 | 4 | 1 | 0 | 0 | 137 | 0 | 1 | 2 | 195 | 4 | 1 |
| FILM, DRAMA & SPORT | 0 | 0 | 0 | 0 | 1 | 4 | 76 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 4 | 76 | 1 | 0 |
| FINANCE | 0 | 0 | 0 | 0 | 1 | 2 | 54 | 2 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 54 | 2 | 0 |
| NEWS | 0 | 0 | 0 | 0 | 4 | 5 | 218 | 5 | 0 | 0 | 0 | 0 | 0 | 4 | 5 | 218 | 5 | 0 |
| NON DIVISION | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| NON DIVISION - BOARD OF MANAGEMENT | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 8 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 8 | 0 |
| NON DIVISION - SALES & MARKETING | 0 | 0 | 0 | 0 | 0 | 0 | 66 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 66 | 1 | 0 |
| OPERATIONAL BROADCAST | 0 | 0 | 4 | 0 | 9 | 9 | 376 | 3 | 0 | 0 | 0 | 4 | 0 | 9 | 9 | 376 | 3 | 0 |
| PCDC | 0 | 0 | 0 | 0 | 0 | 0 | 5 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5 | 0 | 0 |
| PRODUCTION 1 | 0 | 0 | 1 | 0 | 1 | 2 | 124 | 1 | 0 | 0 | 0 | 1 | 0 | 1 | 2 | 124 | 1 | 0 |
| PRODUCTION 2 | 0 | 0 | 1 | 0 | 5 | 3 | 109 | 1 | 0 | 0 | 0 | 1 | 0 | 5 | 3 | 109 | 1 | 0 |
| PRODUCTION 4 | 0 | 0 | 0 | 0 | 0 | 2 | 40 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2 | 40 | 0 | 0 |
| PRODUCTION FACILITIES | 0 | 0 | 2 | 0 | 4 | 13 | 597 | 5 | 0 | 0 | 0 | 2 | 0 | 4 | 13 | 597 | 5 | 0 |
| PROGRAMMING | 0 | 0 | 0 | 0 | 1 | 0 | 28 | 3 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 28 | 3 | 0 |
| PROMOTION | 0 | 0 | 0 | 0 | 1 | 0 | 25 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 25 | 1 | 0 |
| SALES 1 | 0 | 0 | 0 | 0 | 0 | 0 | 19 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 19 | 0 | 0 |
| SALES 2 | 0 | 0 | 1 | 0 | 0 | 0 | 9 | 2 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 9 | 2 | 0 |
+------------------------------------+----+-----+-----+----+----+----+-----+----+----+----+-----+-----+----+----+----+-----+----+----+
答案 0 :(得分:-2)
如果您知道自己拥有的年限,以下查询将以您希望的形式获取数据。如果您希望按特定顺序更改字段列表,或者在示例中省略某些列,则更改字段列表。
select * from rekap_teams.tblrekappendidikan_divisitahunan as t2014 join
rekap_teams.tblrekappendidikan_divisitahunan as t2015 on(DIVISI)
where t2014.year = 2014 and t2015.year=2015
将*替换为列名的顺序。