create table attendance
(user_id int,attendance_code varchar(1),dte date)
*/
truncate table attendance;
insert into attendance values
(1,'a','2016-01-01'),
(1,null,'2016-01-02'),
(1,'a','2016-01-03'),
(1,'a','2016-01-04'),
(1,'a','2016-01-05'),
(2,'a','2016-01-01'),
(2,'a','2016-01-02'),
(2,null,'2016-01-03'),
(2,null,'2016-01-04'),
(2,'a','2016-01-05')
;
select a.user_id,
max(case when day(a.dte) = 1 then case when a.attendance_code is not null then 'On' else 'Off' end end) as day1,
max(case when day(a.dte) = 2 then case when a.attendance_code is not null then 'On' else 'Off' end end) as day2,
max(case when day(a.dte) = 3 then case when a.attendance_code is not null then 'On' else 'Off' end end) as day3,
max(case when day(a.dte) = 4 then case when a.attendance_code is not null then 'On' else 'Off' end end) as day4,
max(case when day(a.dte) = 5 then case when a.attendance_code is not null then 'On' else 'Off' end end) as day5
from attendance a
group by a.user_id;
uはしてみてくださいでしたか?........これを参照してくださいhttp://stackoverflow.com/questions/ 14834290/mysql-query-to-dynamic-rows-to-columnsを変換してクエリを更新するクエリもあります... –
クエリが必要なので、私はうまくいきません。 MAX(IF(abs_code is null、 'ON'、 'OFF'))1日目として MAXのfuncは私にとって役に立たないと思う。 –