2017-07-31 110 views
0

我得到的只是罰款查詢:GROUP_CONCAT與加列

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

我想(也許是太基本)是生成的查詢有一個新的列,例如新列一個與加盟值期間+房間+'最終'。但我不斷收到例外。不知道真正的原因。

什麼我想...

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; 

感謝。

回答

1

您還應該使用字符串連接(不+​​)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; 
+0

謝謝,好像MySQL有點特別... –

0

您可以使用CONCAT_WS來實現這一點。

(」」,列1,列2)AS列名

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

+0

分離器添加到組應該有一個空格。 – Barmar

+0

謝謝我會編輯它 –

0

使用concat進行連接而不是+。並添加testgroup 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;