在MySQL中按varchar分组

时间:2018-12-17 10:44:55

标签: mysql group-by

我有一个查询。

SELECT  
         sa.start_time,
    `Monday`, `Tuesday`, `Wednesday`, `Thursday`, `Friday` 
FROM ( 
  SELECT  ders_saat,
         (CASE WHEN days= 1 THEN d.lesson_name ELSE 0 END) AS `Monday`, 
         (CASE WHEN days= 2 THEN d.lesson_name ELSE 0 END) AS `Tuesday`, 
         (CASE WHEN days= 3 THEN d.lesson_name ELSE 0 END) AS `Wednesday`, 
         (CASE WHEN days= 4 THEN d.lesson_name ELSE 0 END) AS `Thursday`, 
         (CASE WHEN days= 5 THEN d.lesson_name ELSE 0 END) AS `Friday`
      FROM ders_programi
      inner join lessons d on d.id = lesson_id

) AS sums
left join hours sa on sa.start_time = sums.ders_saat

结果:5行

start_time  Monday Tuesday Wednesday Thursday Friday
08:00:00    Math    0         0         0       0
08:50:00    null    null      null      null    null                        
09:40:00    Math    0         0         0       0
10:30:00    Prog    0         0         0       0
10:30:00    0       Geo       0         0       0

我想要的结果:4行

start_time  Monday Tuesday Wednesday Thursday Friday
08:00:00    Math    0         0         0       0
08:50:00    null    null      null      null    null            
09:40:00    Math    0         0         0       0
10:30:00    Prog    Geo       0         0       0

我该怎么做?我已经尝试过group by start_timeconcat,但是没有用。

我用4行分组返回。但最后一行:10:30:00 Prog 0 0 0 0

1 个答案:

答案 0 :(得分:0)

~/.linuxbrew/lib/boost