将行转换为列

时间:2019-03-11 18:30:25

标签: mysql sql

我正在尝试将行转换为列,我有如下所示的sql数据

enter image description here

我想为每个学生在同一科目前面的第二栏中显示获得的下一次考试成绩。像下面 预先感谢。

enter image description here

2 个答案:

答案 0 :(得分:0)

感谢大家,通过左联接解决了问题。

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)

答案 1 :(得分:0)

要将行(行)转换为列,请使用以下代码:

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