00:00格式的总小时数表单mySQL表
我有一个Varchar2数据类型的表列,其数据格式为“00:00”。现在,我想使用SUM()函数添加此列的总计数字。请建议在单个select语句中添加总计数字的方法00:00格式的总小时数表单mySQL表,mysql,Mysql,我有一个Varchar2数据类型的表列,其数据格式为“00:00”。现在,我想使用SUM()函数添加此列的总计数字。请建议在单个select语句中添加总计数字的方法 客户代码 小时 1 22:30 2 11:20 2 10:20 假设您的数据库架构如下所示: CREATE TABLE IF NOT EXISTS `test` ( `id` int(11) unsigned AUTO_INCREMENT PRIMARY KEY, `code` int(11) unsigned
客户代码
小时
1
22:30
2
11:20
2
10:20
假设您的数据库架构如下所示:
CREATE TABLE IF NOT EXISTS `test` (
`id` int(11) unsigned AUTO_INCREMENT PRIMARY KEY,
`code` int(11) unsigned NOT NULL,
`content` varchar(10) NOT NULL
) DEFAULT CHARSET=utf8;
INSERT INTO `test` (`id`, `code`, `content`) VALUES
('1', '1', '10:30'),
('2', '1', '5:40'),
('3', '2', '22:30'),
('4', '3', '15:20');
然后,您可以得到如下结果:
SELECT
code,
SEC_TO_TIME(SUM(TIME_TO_SEC(CAST(content AS TIME)))) AS total
FROM
test
GROUP BY code;
这会给你
code total
1 16:10:00
2 22:30:00
3 15:20:00
假设您的数据库架构如下所示:
CREATE TABLE IF NOT EXISTS `test` (
`id` int(11) unsigned AUTO_INCREMENT PRIMARY KEY,
`code` int(11) unsigned NOT NULL,
`content` varchar(10) NOT NULL
) DEFAULT CHARSET=utf8;
INSERT INTO `test` (`id`, `code`, `content`) VALUES
('1', '1', '10:30'),
('2', '1', '5:40'),
('3', '2', '22:30'),
('4', '3', '15:20');
然后,您可以得到如下结果:
SELECT
code,
SEC_TO_TIME(SUM(TIME_TO_SEC(CAST(content AS TIME)))) AS total
FROM
test
GROUP BY code;
这会给你
code total
1 16:10:00
2 22:30:00
3 15:20:00
以上ans经过了大量优化,但您也可以使用
SELECT code,Replace(ROUND((SUM(substr(content,1,2) * 60) + SUM(substr(content,4,5)) ) / 60 , 2) ,'.',':') as total_hrs FROM test group by code
如果您不想使用许多预定义功能,这可能会有所帮助。以上ans经过了大量优化,但您可以使用
SELECT code,Replace(ROUND((SUM(substr(content,1,2) * 60) + SUM(substr(content,4,5)) ) / 60 , 2) ,'.',':') as total_hrs FROM test group by code
如果您不想使用很多预定义函数,这可能会有所帮助