在mySQL中格式化返回值至小数点后2位
我有下面的查询,它从我的数据库中检索数据,它将所有值相加并将其除以60以将其转换为小时:在mySQL中格式化返回值至小数点后2位,mysql,sql,formatting,Mysql,Sql,Formatting,我有下面的查询,它从我的数据库中检索数据,它将所有值相加并将其除以60以将其转换为小时: SELECT date(att.`date`) as dtDate, (select sum(early_leave) from `attendance` where `attendance`.early_leave > 0 and date(`attendance`.`date`) = date(att.date)) / 60 as EL FROM `attendance` as att w
SELECT date(att.`date`) as dtDate,
(select sum(early_leave) from `attendance` where `attendance`.early_leave
> 0 and date(`attendance`.`date`) = date(att.date)) / 60 as EL
FROM `attendance` as att
where date(att.`date`) BETWEEN '2018-08-9' and '2018-08-23'
group by date(att.`date`)
order by date(att.`date`)
它的输出如下所示:
现在,我希望它的格式为小数点后2位,所以我要:
SELECT date(att.`date`) as dtDate,
(select format(sum(early_leave),2) from `attendance` where `attendance`.early_leave
> 0 and date(`attendance`.`date`) = date(att.date)) / 60 as EL
FROM `attendance` as att
where date(att.`date`) BETWEEN '2018-08-9' and '2018-08-23'
group by date(att.`date`)
order by date(att.`date`)
但为什么有些输出是这样的:
我希望2018-08-10的第二个值是
28
,以此类推。有人知道为什么会这样吗?或者我的格式设置正确吗?您需要执行格式设置,因为最后一件事是除以60将导致结果中出现额外的小数位:
format((select sum(early_leave) from `attendance` where `attendance`.early_leave
> 0 and date(`attendance`.`date`) = date(att.date)) / 60, 2) as EL
如果不想显示不需要的小数位数,只需添加0
format((select sum(early_leave) from `attendance` where `attendance`.early_leave
> 0 and date(`attendance`.`date`) = date(att.date)) / 60, 2)+0 as EL
例如:
select format(2.00,2)+0, format(1.5,2)+0, format(1.55,2)+0
输出:
2, 1.5, 1.55
您需要执行格式
,因为最后一件事是除以60将导致结果中额外的小数位数:
format((select sum(early_leave) from `attendance` where `attendance`.early_leave
> 0 and date(`attendance`.`date`) = date(att.date)) / 60, 2) as EL
如果不想显示不需要的小数位数,只需添加0
format((select sum(early_leave) from `attendance` where `attendance`.early_leave
> 0 and date(`attendance`.`date`) = date(att.date)) / 60, 2)+0 as EL
例如:
select format(2.00,2)+0, format(1.5,2)+0, format(1.55,2)+0
输出:
2, 1.5, 1.55
它将所有值相加,然后除以60,将其转换为小时
是否确实要将某个值除以60以将其转换为小时?这假设早退存储时间为分钟。是这样吗
关于格式。您需要将第二列作为一个整体进行格式化:
(select format(sum(early_leave),2) from `attendance` where `attendance`.early_leave
> 0 and date(`attendance`.`date`) = date(att.date)) / 60
换成
CAST((select format(sum(early_leave),2) from `attendance` where `attendance`.early_leave
> 0 and date(`attendance`.`date`) = date(att.date)) / 60 AS DECIMAL(12,2))
然而,这并不能解决你的问题。如果有些结果正确,有些则不正确,那么问题很可能是计算或源数据
另一方面,原始查询有点混乱。与其使用子查询,更好的解决方案是联接该表。类似这样的东西(未经测试,但它证明了我的观点)
它将所有值相加,然后除以60,将其转换为小时
是否确实要将某个值除以60以将其转换为小时?这假设早退存储时间为分钟。是这样吗
关于格式。您需要将第二列作为一个整体进行格式化:
(select format(sum(early_leave),2) from `attendance` where `attendance`.early_leave
> 0 and date(`attendance`.`date`) = date(att.date)) / 60
换成
CAST((select format(sum(early_leave),2) from `attendance` where `attendance`.early_leave
> 0 and date(`attendance`.`date`) = date(att.date)) / 60 AS DECIMAL(12,2))
然而,这并不能解决你的问题。如果有些结果正确,有些则不正确,那么问题很可能是计算或源数据
另一方面,原始查询有点混乱。与其使用子查询,更好的解决方案是联接该表。类似这样的东西(未经测试,但它证明了我的观点)
感谢这一点,我也这样发现了它(第一个),我现在的问题是它是否是一个整数,比如17.00
return应该是17,if has.50
return应该是.5。如果你能在这方面提供更多帮助,请。谢谢。'@ramj查看我的编辑,它将允许您删除不需要的小数位数谢谢,我也是这样发现的(第一个),我现在的问题是它是否是一个整数,比如17.00
返回值应该是17,如果有.50
返回值应该是.5。如果你能在这方面提供更多帮助,请。谢谢。'@ramj查看我的编辑,它将允许您删除不需要的小数点