Search code examples
mysqlgroup-bygroup-concat

Group_concat with added columns


I got a query that works just fine:

SELECT Course, Period, Room, Location, TeacherId, GROUP_CONCAT(StudentId) as Students 
FROM cursosv2 
GROUP BY Course, Period, Room, Location, TeacherID;

What I want (maybe too basic) is in the resulting query to have a new column, like NEW COLUMN A with the joined values of period+room+'final'. But I keep getting exceptions. Dont know the real reason.

What am I trying...

SELECT Course, Period, Room, Period+' '+Room+' Final' as test, Location, TeacherId, GROUP_CONCAT(StudentId) as Students 
FROM cursosv2 
GROUP BY Course, Period, Room, Location, TeacherID;

Thanks.


Solution

  • You should also use concat for string concatenation (not + ) and add to group by

     SELECT Course, Period, Room, concat( Period,' ',Room,' Final') as test, Location, TeacherId, GROUP_CONCAT(StudentId) as Students 
    
     FROM cursosv2 
     GROUP BY Course, Period, test, Room, Location, TeacherID;