如何将mysql行结果更改为列

时间:2017-10-04 07:03:03

标签: php mysql select join

我有一个像这样的SQL查询:

SELECT mahasiswa.nim, mahasiswa.nama, nilai_pretest.nilai_pretest, nilai_session.nilai_session FROM nilai_session 
INNER JOIN materi ON materi.id_materi=nilai_session.id_materi
INNER JOIN nilai_pretest ON nilai_pretest.id_nilai_pretest=nilai_session.id_nilai_pretest
INNER JOIN pengambilan ON pengambilan.id_pengambilan=nilai_pretest.id_pengambilan 
INNER JOIN mahasiswa ON mahasiswa.nim= pengambilan.nim
INNER JOIN pengampu ON pengampu.id_pengampu= pengambilan.id_pengampu 
WHERE pengampu.id_pengampu=4

结果是:

+-----------+-------+---------------+---------------+ 
| nim        | nama  | nilai_pretest | nilai_session | 
+------------+-------+---------------+---------------+ 
| 1215051173 | nama1 | 70            | 100           |
| 1215051173 | nama1 | 70            | 35            |
| ...        | ....  |               |               |
+------------+-------+---------------+---------------+

但我想要这样的结果:

+-----------+-------+---------------+---------+---------+-----------+
| nim        | nama  | nilai_pretest | materi1 | materi2 | materi... |
+------------+-------+---------------+---------+---------+-----------+
| 1215051173 | nama1 | 70            | 100     | 35      | ...       |
| ...        | ....  | ...           | ...     | ...     | ...       |
+------------+-------+---------------+---------+---------+-----------+

我该如何处理?我尝试了很多方法,但仍然无法正常工作。

=============================================== =

我的结构表是:

** nilai_session表

+-----------------+---------------+------------------+-----------+
| id_nilai_session | nilai_session | id_nilai_pretest | id_materi |
+------------------+---------------+------------------+-----------+
| 8                | 100           | 5                | ...       |
| 9                | 35            | 5                | ...       |
| ...              | ....          | ...              | ...       |
+------------------+---------------+------------------+-----------+

** materi table

+-----------+-------------+-----+
| id_materi | judul_materi | ... |
+-----------+--------------+-----+
| 5         | 6            | ... |
| ...       | ....         | ... |
+-----------+--------------+-----+

** nilai_pretest表

+-----------------+--------------+---------------+ 
| id_nilai_pretest | id_pengambil | nilai_pretest |
+------------------+--------------+---------------+
| 5                | 6            | 70            |
| ...              | ....         | ...           |
+------------------+--------------+---------------+

** pengambilan表

+---------------+-------------+------------+
| id_pengambilan | id_pengampu | nim        |
+----------------+-------------+------------+
| 6              | 4           | 1215051173 |
| ...            | ....        | ...        |
+----------------+-------------+------------+

** mahasiswa table

+-----------+-------+
| nim        | nama  |
+------------+-------+
| 1215051173 | nama1 |
| 1215051112 | nama2 |
| ...        | ....  |
+------------+-------+

** pengampu表

+------------+---------------+------+
| id_pengampu | id_matakuliah | ...  |
+-------------+---------------+------+
| 4           | TIK1225       | ...  |
| 5           | TIK1204       | ...  |
| ...         | ....          | ...  |
+-------------+---------------+------+

感谢您的关注。

0 个答案:

没有答案