使用CONCAT和GROUP_CONCAT mysql查询将行转换为coumns dinamiacally用于某些表

时间:2017-02-16 19:11:17

标签: mysql sql concat group-concat

跟随Mysql query to dynamically convert rows to columns on the basis of two columns, 我有4个表将dinamiacally行转换为列为RESULT

表A

| id |  stages |
-----+----------
| 1  | Stage A |
| 2  | Stage B |
| 3  | Stage C |

表B

| id_b | code | lessons     |
-------+------+--------------
| 10   | FIS  | Physics     |
| 11   | MAT  | Mathematics |
| 12   | KIM  | Chemistry   |
| 13   | BIO  | Biology     |

表C

|id_c|  students  |
-----+-------------
| 20 | student 20 |
| 21 | student 21 |
| 22 | student 22 |
| 23 | student 23 |
| 24 | student 24 |
| 25 | student 25 |
| 26 | student 26 |
| 27 | student 27 |

表D

| id_d | id_a | id_b | id_c | value_m | value_n |
-------+------+------+------+---------+----------
| 201 |   1   |  11  |  20  |    71   |   73    |
| 203 |   1   |  13  |  20  |    80   |   87    |
| 204 |   2   |  10  |  21  |    72   |   75    |
| 206 |   2   |  13  |  21  |    76   |   78    |
| 208 |   2   |  12  |  22  |    72   |   78    |
| 209 |   2   |  13  |  22  |    76   |   80    |
| 210 |   3   |  11  |  23  |    73   |   71    |
| 211 |   3   |  12  |  23  |    71   |   77    |
| 212 |   3   |  13  |  23  |    78   |   81    |
| 213 |   3   |  10  |  24  |    70   |   81    |
| 214 |   3   |  11  |  25  |    71   |   82    |
| 215 |   3   |  12  |  26  |    74   |   83    |
| 215 |   3   |  13  |  27  |    78   |   80    |

RESULT

|  students  | FIS_1 | MAT_1 | KIM_1 | BIO_1 | FIS_2 | MAT_2 | KIM_2 | BIO_2 | FIS_3 | MAT_3 | KIM_3 | BIO_3 |
-------------+-------+-------+-------+-------+--------+------+-------+-------+-------+-------+-------+--------
| student 20 |       | 71,73 |       | 80,87 |       |       |       |       |       |       |       |       |
| student 21 |       |       |       |       | 72,75 | 76,78 |       |       |       |       |       |       |
| student 22 |       |       |       |       |       |       | 72,78 | 76,80 |       |       |       |       |
| student 23 |       |       |       |       |       |       |       |       |       | 73,71 | 71,77 | 78,81 |
| student 24 |       |       |       |       |       |       |       |       | 70,81 |       |       |       |
| student 25 |       |       |       |       |       |       |       |       |       | 71,82 |       |       |
| student 26 |       |       |       |       |       |       |       |       |       |       | 74,83 |       |
| student 27 |       |       |       |       |       |       |       |       |       |       |       | 78,80 |    

其中 FIS_1,MAT_1,... FIS_2,MAT_2 ..,FIS_3,MAT_3 .. CONCAT 来自表B kode和TABLE A id,

MAT_1,BIO_1 ...来自表D的CONCAT value_m和value_n

它的可用性?我怎么能用GROUP_CONCAT mysql查询呢?我已经尝试了但仍未成功..

感谢您的推进..

0 个答案:

没有答案