Php two-dimensional matrix

I have a sql table. The table shows studentID, Subject_Name, and Marks. Hope you can understand the data.

Now I need to show it at the front end, showing studentID vertically along the Y axis and subject_Name horizontally along the X axis. Characters should appear as the body of the table.

I use php as a server language.

help.

+5
source share
1 answer

I believe that a simple summary query will give you the result set you want:

SELECT studentID, SUM(CASE WHEN Subject_Name = 'CHEMISTRY' THEN Marks ELSE 0 END) AS `CHEMISTRY`, SUM(CASE WHEN Subject_Name = 'BIOLOGY' THEN Marks ELSE 0 END) AS `BIOLOGY`, SUM(CASE WHEN Subject_Name = 'ENGLISH' THEN Marks ELSE 0 END) AS `ENGLISH`, SUM(CASE WHEN Subject_Name = 'MATH' THEN Marks ELSE 0 END) AS `MATH` FROM students GROUP BY studentID 

You can replace and add / subtract the sample columns that I gave, with the names of the actual course subjects in your table.

Follow the link below for a working demo:

SQLFiddle

+1
source

All Articles