我正在寻找一个想要更新我的表列的想法:当计数前5行然后列='class'而下一行将是'Exam'然后再次在这6行之后它会相同的5和下一个1直到最后。
Sample data:
2016-01-18 Class
2016-01-19 Class
2016-01-20 Class
2016-01-21 Class
2016-01-23 Class
2016-01-24 Exam
2016-01-25 Class
2016-01-26 Class
2016-01-27 Class
2016-01-28 Class
2016-01-30 Class
2016-01-31 Exam
2016-02-01 Class
2016-02-02 Class
2016-02-03 Class
2016-02-04 Class
2016-02-06 Class
2016-02-07 Exam
答案 0 :(得分:3)
您可以使用此代码:
UPDATE test t
INNER JOIN (SELECT sdate, @row :=@row +1 AS row,mod(@row,6) AS num
FROM test, (SELECT @row := 0) r) AS tt
ON t.sdate = tt.sdate
SET t.name =
CASE num
WHEN 0 THEN 'Exam'
ELSE 'Class'
END;
如果还有其他字段day_off
,我们需要仅针对day_off =''的字段进行此更新:
UPDATE test2 t
INNER JOIN (SELECT sdate, @row :=@row +1 AS row,mod(@row,6) AS num
FROM test2, (SELECT @row := 0) r
WHERE day_Off = '') AS tt
ON t.sdate = tt.sdate
SET t.name =
CASE num
WHEN 0 THEN 'Exam'
ELSE 'Class'
END;