我有一张
employee
表,其中包含:
emp id Sum
------ ---
1 7
2 6
我想要一个 SQL 查询来获取
Sum
除以 8 时的商和余数。
SELECT
sum,
sum / 8 AS result,
sum div 8 AS quotient,
sum mod 8 AS remainder
FROM employee
输出示例:
sum | result | quotient | remainder
7 | 0.8750 | 0 | 7
6 | 0.7500 | 0 | 6
9 | 1.1250 | 1 | 1
10 | 1.2500 | 1 | 2
11 | 1.3750 | 1 | 3
12 | 1.5000 | 1 | 4
13 | 1.6250 | 1 | 5
14 | 1.7500 | 1 | 6
15 | 1.8750 | 1 | 7
16 | 2.0000 | 2 | 0
您的商的返回类型是什么?如果您不关心它是浮点数还是整数(整数)。你可以试试这个。
SELECT
(sum / 8) AS qoutient,
(sum % 8) AS reminder
FROM employee
可以使用mysql函数DIV来获取qoutient (http://dev.mysql.com/doc/refman/5.0/en/arithmetic-functions.html#operator_div):
SELECT 14 DIV 3
将返回4
应该可以解决问题
至于剩下的,其他人已经回复了
您可以使用
%
运算符来获取余数。这是一个例子。
SELECT Round(17 / 4) -- quotient without decimal
SELECT 17 % 4 -- remainder
对于我的 PL/SQL 函数,我通常使用:
SELECT trunc(sum/8) --- Quotient
SELECT mod(sum/8) -- Remainder
使用floor函数,从dual中选择floor(column_name/divisor)