-4

I am trying to convert rows to columns, i have sql data like below

enter image description here

And I want to display obtained marks for next exam in second column in front of same subject for every student. like below Thanks in Advance.

enter image description here

nofinator
  • 2,906
  • 21
  • 25
Amaan Khan
  • 181
  • 2
  • 3
  • 15
  • 1
    Possible duplicate of [MySQL pivot row into dynamic number of columns](https://stackoverflow.com/questions/12004603/mysql-pivot-row-into-dynamic-number-of-columns) – nofinator Mar 11 '19 at 18:38
  • 3
    There's far too many blurry pictures here and not enough in the way of an attempt to solve the problem. We can always fix your query, but we can't do anything with a screenshot. – tadman Mar 11 '19 at 18:40
  • What have you tried so far??? – Eric Mar 11 '19 at 19:17

2 Answers2

0

Thanks Everyone, Its solved by left join.

SELECT 
  sm.`student_id`, sm.`subject_id`, s.`name` AS subject_name, sm.`marks_obtained` AS e_11 , e2.`marks_obtained` AS e_14
FROM
  ims_studentmarks sm INNER JOIN ims_subject s ON s.`id` = sm.`subject_id`
LEFT JOIN ims_studentmarks e2 ON e2.`subject_id` = sm.`subject_id` AND e2.`student_id` = sm.`student_id` AND e2.`exam_id` = 14


WHERE sm.`exam_id` = 11 
  AND sm.student_id IN (1789)
Amaan Khan
  • 181
  • 2
  • 3
  • 15
0

To convert line (row) to columns, use this code:

SELECT my_column_id,GROUP_CONCAT(my_column_name) FROM my_table GROUP BY my_column_id as All_Columns_Name_Asign_to_Column_id