在mysql中转换小时为hh:mm

muk1a3rh  于 2022-12-26  发布在  Mysql
关注(0)|答案(2)|浏览(154)

我的数据如下所示,包含电子邮件和工作时间(以小时格式表示),预计输出的working_hours为hh:mm:ss

select d.email,TRUNCATE(sum(e.hours),2) as "working_hours"
from users d inner join proj_time e on d.id=e.user_id
WHERE start_time BETWEEN CURDATE() - INTERVAL 1 DAY
AND CURDATE() - INTERVAL 1 SECOND
group by d.id,d.email

| 电子邮件|工作时数|
| - ------| - ------|
| vij@sic.com | 5.77|
| skiol@sic.com | 4.24|
| olps@sic.com | 2.69|
| dvia@sic.com | 6.12|
我尝试通过以下查询更改working_hours,但收到前导零,请求输出从working_hours输出中删除前导零(00:)

select d.email,SEC_TO_TIME(TRUNCATE(sum(e.hours * 60),2)) as "working_hours"
from users d inner join proj_time e on d.id=e.user_id
WHERE start_time BETWEEN CURDATE() - INTERVAL 1 DAY
AND CURDATE() - INTERVAL 1 SECOND
group by d.id,d.email

| 电子邮件|工作时数|
| - ------| - ------|
| vij@sic.com | 00时05分46秒20|
| skiol@sic.com | 00时04分14秒40|
| olps@sic.com | 00时02分41秒40|
| dvia@sic.com | 00时06分07秒20分|

    • 预期输出如下所示**

| 电子邮件|工作时数|
| - ------| - ------|
| vij@sic.com | 05时46分|
| skiol@sic.com | 04时14分|
| olps@sic.com | 凌晨2点41分|
| dvia@sic.com | 6点7分|

gcuhipw9

gcuhipw91#

+非常感谢你的帮助。我得到了我问题的答案,
第一个月

ev7lccsx

ev7lccsx2#

CREATE TABLE test (email VARCHAR(255), working_hours DECIMAL(10,2));
INSERT INTO test VALUES
('vij@sic.com',     5.77),
('skiol@sic.com',   4.24),
('olps@sic.com',    2.69),
('dvia@sic.com',    6.13);
SELECT email,
       SEC_TO_TIME(SUM(working_hours * 3600)) total_time,
       SUBSTRING_INDEX(SEC_TO_TIME(SUM(working_hours * 3600)), ':', 2) truncated,
       SUBSTRING_INDEX(SEC_TO_TIME(SUM(working_hours * 3600 + 30)), ':', 2) rounded,
       DATE_FORMAT(SEC_TO_TIME(SUM(working_hours * 3600)), '%h:%i') formatted_truncated
FROM test
GROUP BY 1;

| 电子邮件|总时间|截短的|圆整的|格式化_截断|
| - ------| - ------| - ------| - ------| - ------|
| vij@sic.com | 05时46分12秒|05时46分|05时46分|05时46分|
| skiol@sic.com | 04时14分24秒|04时14分|04时14分|04时14分|
| olps@sic.com | 02时41分24秒|凌晨2点41分|凌晨2点41分|凌晨2点41分|
| dvia@sic.com | 06时07分48秒|6点7分|06时08分|6点7分|
fiddle

相关问题