0

I have a table like this:

mysql> select * from exams;
+------+------+------+-------+
| id   | name | exam | score |
+------+------+------+-------+
|   1  |  Bob |   1  |   70  |
|   2  |  Bob |   2  |   77  |
|   3  |  Bob |   3  |   71  |
|   4  |  Bob |   4  |   70  |
|   5  |  Sue |   1  |   89  |
|   6  |  Sue |   2  |   87  |
|   7  |  Sue |   3  |   88  |
|   8  |  Sue |   4  |   89  |
+------+------+------+-------+

and I want to pivot the table by exam column so as to create 1 row for each student and 1 column for each exam.

I would like this output:

   name     exam1      exam2      exam3      exam4   

   Bob       70         77         71         70     
   Sue       89         87         88         89     

I am using an old version of mysql, mysql 5.x

I would need some clues to achive that results.

Raphael
  • 92
  • 10
  • Do you have a fixed list of exams, or the columns need to be defined dynamically? – GMB Jun 18 '20 at 17:19
  • 1
    Consider handling issues of data display in application code. Failing that, see https://meta.stackoverflow.com/questions/333952/why-should-i-provide-a-minimal-reproducible-example-for-a-very-simple-sql-query – Strawberry Jun 18 '20 at 17:19

0 Answers0