How can I change from
SID Name Math English French
1 Sam 16 17 19
2 Tom 18 14 12
3 Al 90 33 2
to:
SID subject Mark
1 Math 16
1 English 17
1 French 19
2 Math 18
2 English 14
2 French 12
3 Math 90
3 English 33
3 French 2
using SQL (MYsql and MS Access if possible) ?
Thanks.
But the root cause of your problem is a wrong database design. Those subjects shouldn’t be columns in the first place and should be stored in a table very much like your desired output.
Edit
So what does it do?
Returns the
sidcolumn, a “virtual” column with the hardcoded value'Math'that is given the namesubject. As you have not stored the value'Math'somewhere, this had to be hardcoded. Then at last it also selects the columnmathusing the namemarkinstead. Note the difference betweenmathand'Math'– one is a column the other one a string literal because of the single quotes.This is done for all three subjects (if you had four subjects, you’d need four parts in the UNION)
The UNION ALL combines all three SELECTs into one single query. andr solution (which has been downvoted by someone who didn’t understand it) makes this even clearer by explicitely putting that into a derived table (or inline view).
Run each SELECT on its own to see what the individual parts are doing.
The part
as markis called a “column alias” and can also be used to retrieve columns with the same name from different tables in a join and still have unique names in the result set.