數學運算

標準 ANSI SQL-92 提供以下運算:

+
-
*
/
% 餘數

餘數為除法後剩整數,這不在標準的 ANSI SQL 中,但大部份的資料庫都會支援這運算元。以下列出有用的函數,雖不在 ANSI SQL-92 提供,但一般資料庫也會支援他們。

ABS(x) 傳回 x 絕對值
SIGN(x) returns the sign of input x as -1, 0, or 1 (negative, zero, or positive respectively)
MOD(x,y) 餘數 - returns the integer remainder of x divided by y (同 x%y)
FLOOR(x) 傳回小或等於 x 的最大整數值
CEILING(x) or CEIL(x) 傳回大或等於 x 的最小整數值
POWER(x,y) x的 y 次方
ROUND(x) 把 x 四捨五入為整數
ROUND(x,d) 四捨五入取到小數 d 位
SQRT(x) x 開平方

For example:

SELECT round(salary), firstname
FROM employee_info

傳回薪資取到整數位。

Use these tables for the exercises
items_ordered
customers

練習題

1) 使用 items_ordered 表,列出物品(item),及單價(非價格)。

 

解答