Group_concat 添加了列

Group_concat with added columns

我得到一个运行良好的查询:

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

我想要的(可能太基础了)是在生成的查询中有一个新列,比如 NEW COLUMN A 和 period+room+'final' 的连接值。但我不断收到例外。不知道真正的原因。

我在尝试什么...

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;

谢谢。

您可以使用 CONCAT_WS 来实现。

(' ', Column1, column2) AS column_name

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

https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_concat-ws

使用 concat 连接而不是 +。并将 test 添加到 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, Room, test, Location, TeacherID;

您还应该使用 concat 进行字符串连接(不是 + )并添加到分组依据

 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;