2
我想將行轉換爲列(星期幾名稱),但每行只有一個條目,而其餘列是空的(對於教師和時間段)。將行轉換爲MySQL列
表結構:
後續查詢的輸出:
和我的查詢是:
SELECT
/* Employee section */
CONCAT(emp.first_name, " ",emp.middle_name,"", emp.last_name) as employeeFullName,
/* Timetable Entry section */
CONCAT(ct.start_time," - ",ct.end_time) as Slots,
if(wd.name='Monday',CONCAT(sb.name," - ",bt.name),null) Monday,
if(wd.name='Tuesday',CONCAT(sb.name," - ",bt.name),null) Tuesday,
if(wd.name='Wednesday',CONCAT(sb.name," - ",bt.name),null) Wednesday,
if(wd.name='Thurseday',CONCAT(sb.name," - ",bt.name),null) Thurseday,
if(wd.name='Friday',CONCAT(sb.name," - ",bt.name),null) Friday,
if(wd.name='Saturday',CONCAT(sb.name," - ",bt.name),null) Saturday,
if(wd.name='Sunday',CONCAT(sb.name," - ",bt.name),null) Sunday
FROM `timetable_entries` te
left Join `weekdays` wd ON te.weekday_id=wd.id
left Join `employees` emp ON te.employee_id=emp.id
left Join `class_timings` ct ON te.class_timing_id=ct.id
left Join `batches` bt ON te.batch_id=bt.id
left Join `subject` sb ON te.subject_id=sb.id
WHERE te.employee_id is not null
Group By te.class_timing_id,te.employee_id
我很舒服「轉換成列」不是你想要的。這與關係數據庫的原理相矛盾。 –
感謝您的回覆,抱歉讓人困惑,但我認爲問題從圖片和查詢中清楚! – sefburhan
我沒有時間詳細闡述,但我認爲你需要從課堂時間開始查詢,並從08:00,09:00等開始每節課。從那裏你可以得到課程的細節,一週工作日,員工等 –