dimanche 3 juillet 2016

how to combine two columns in mysql


its a doubt. I have two tables one table contains student name,roll number,subject id for example

JOHN  22  1,2  
SAM   33  2,1  
ROY   32  1,2

here 1,2 is my subject id. where one belongs to subject1 & 2 belongs to subject2 both are optional for a particular student.

And the second table contain subject1 and subject2 mark.

i want to combine both subjects mark into one column for corresponding students. pls help me

SELECT table1.roll_num,table1.student_name, 
    CASE 
        WHEN FIND_IN_SET(8, 'table1.subject_id') THEN sub1 
        ELSE sub2 
    END AS subject_mark
FROM table2 
JOIN table1 ON table1.student_id = table2.student_id 
WHERE division = 'A 
ORDER BY table1.roll_num asc

Aucun commentaire:

Enregistrer un commentaire