在SQL中选择/转换输出为整数 [英] Selecting/casting output as integer in SQL

查看:574
本文介绍了在SQL中选择/转换输出为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网站,要求我显示用户输入每天平均数量的图表。我有一个SQL查询已将此信息返回给我:

I'm working on a site that requires me to display a graph of the average number per day of a user input. I have a SQL query already that returns this info to me:

SELECT sum(number)/count(number) as average, date FROM stats WHERE * GROUP BY date

这给了我正在寻找的结果,但结果给出三位小数精度。我想要回合这个数字。当然,我可以在PHP或我的模板引擎中完成它,但我很好奇是否有办法在数据库中完成所有这些。

This gives me the result I am looking for, but the result is given with three decimals precision. I want to round of this number. I could do it in PHP or my template engine, of course, but I was curious if there was a way to do this all in the database.

有没有办法将输出转换为整数(在MySQL中)?

Is there a way to cast an output as an integer (in MySQL)?

推荐答案

SELECT 
  CAST(sum(number)/count(number) as UNSIGNED) as average, 
  date 
FROM stats 
WHERE * 
GROUP BY date

这篇关于在SQL中选择/转换输出为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆