添加了列的Group_concat

时间:2017-07-31 16:56:48

标签: mysql group-by group-concat

我得到的查询工作正常:

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

我想要的(可能太基本)是在结果查询中有一个新列,例如NEW COLUMN A,其中包含句点+ 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;

感谢。

3 个答案:

答案 0 :(得分:1)

您还应该使用concat进行字符串连接(而非+)并添加到

{{1}}

答案 1 :(得分:0)

您可以使用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

答案 2 :(得分:0)

使用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;