MySQL 如何获得除法的商和余数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5603271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the quotient and remainder of division
提问by vvvi
I have one employee
table which contains:
我有一张employee
表,其中包含:
emp id Sum
------ ---
1 7
2 6
I want a SQL query for getting the quotient and remainder when dividing the Sum
with 8.
我想要一个 SQL 查询,用于在除以Sum
8时获取商和余数。
回答by Salman A
Use integer divisionand modoperators to get the quotient and remainder:
SELECT
emp_id,
sum,
sum / 8 AS Result,
sum div 8 AS Quotient,
sum mod 8 AS Remainder
FROM employee
emp_id sum Result Quotient Remainder
1 7 0.8750 0 7
2 6 0.7500 0 6
3 9 1.1250 1 1
4 10 1.2500 1 2
5 11 1.3750 1 3
6 12 1.5000 1 4
7 13 1.6250 1 5
8 14 1.7500 1 6
9 15 1.8750 1 7
10 16 2.0000 2 0
回答by Igor
You can use the mysql function DIV to get the qoutient (http://dev.mysql.com/doc/refman/5.0/en/arithmetic-functions.html#operator_div) :
您可以使用 mysql 函数 DIV 来获取 qoutient ( http://dev.mysql.com/doc/refman/5.0/en/arithmetic-functions.html#operator_div):
SELECT 14 DIV 3
will return 4
将返回 4
It should do the trick
它应该可以解决问题
As for the remainder, others have replied
至于剩下的,其他人已经回复了
回答by ace
What will be the return type of your qoutient? If you don't care if its a floating point or an integer(whole number). You can try this.
您的 qoutient 的返回类型是什么?如果您不在乎它是浮点数还是整数(整数)。你可以试试这个。
SELECT
(sum / 8) AS qoutient,
(sum % 8) AS reminder
FROM employee
回答by Paolo Falabella
you can use the %
operator to get the remainder. Here's an example.
您可以使用%
运算符来获取余数。这是一个例子。
SELECT Round(17 / 4) -- quotient without decimal
SELECT 17 % 4 -- remainder
回答by Viji
use floor function, select floor(column_name/divisor) from dual
使用 floor 函数,从 dual 中选择 floor(column_name/divisor)