回答編集履歴

1

TIME 型の計算方法を修正

2020/05/08 05:04

投稿

hoshi-takanori
hoshi-takanori

スコア7895

test CHANGED
@@ -2,7 +2,13 @@
2
2
 
3
3
 
4
4
 
5
+ 修正: なお、MySQL では TIME 型の SUM は秒に変換して計算する必要があるようです。
6
+
7
+ 参考: [MySQLのtime型の合計 - Qiita](https://qiita.com/niiyz/items/df45e886aa24d2fcaba3)
8
+
9
+
10
+
5
- テーブル定義
11
+ テーブル定義 (デバッグ用に user_id = 3 の work_date を変更しました。)
6
12
 
7
13
 
8
14
 
@@ -30,9 +36,9 @@
30
36
 
31
37
  insert into tb_user_work_time values (2, '09:00:00', '2020-03-02');
32
38
 
33
- insert into tb_user_work_time values (3, '08:00:00', '2020-03-01');
39
+ insert into tb_user_work_time values (3, '08:30:00', '2020-03-01');
34
40
 
35
- insert into tb_user_work_time values (3, '10:00:00', '2020-03-02');
41
+ insert into tb_user_work_time values (3, '09:30:00', '2020-03-02');
36
42
 
37
43
  ```
38
44
 
@@ -48,15 +54,15 @@
48
54
 
49
55
  user_id,
50
56
 
51
- sum(work_time - '80000') as total_overtime
57
+ sec_to_time(sum(time_to_sec(work_time) - time_to_sec('8:00:00'))) as total_overtime
52
58
 
53
59
  from tb_user_work_time
54
60
 
55
- where work_time > '80000'
61
+ where work_time > cast('8:00:00' as time)
56
62
 
57
63
  group by user_id
58
64
 
59
- having total_overtime >= '20000';
65
+ having total_overtime >= cast('2:00:00' as time);
60
66
 
61
67
  ```
62
68
 
@@ -74,9 +80,9 @@
74
80
 
75
81
  +---------+----------------+
76
82
 
77
- | 2 | 50000 |
83
+ | 2 | 05:00:00 |
78
84
 
79
- | 3 | 20000 |
85
+ | 3 | 02:00:00 |
80
86
 
81
87
  +---------+----------------+
82
88