SQL FLOOR函数
SQL中的FLOOR数值函数返回小于或等于给定数字的最大整数值。
FLOOR函数的语法
语法1: 此语法使用SQL表的列名与FLOOR函数结合使用:
SELECT FLOOR(Integer_Column_Name) AS Alias_Name FROM Table_Name;
在这个第一个语法中,我们必须指定我们要执行FLOOR数值函数的整数列的名称。
语法2: 这个语法使用FLOOR函数与整数或小数值:
SELECT FLOOR(Decimal_Number);
FLOOR函数示例
示例 1: 此示例返回指定数字的底数:
SELECT FLOOR(0.5) AS floor_Value_of_0.5;
输出:
floor_Value_of_0.5
---
0
示例2: 此示例返回指定数字的向下取整值:
SELECT floor(10.9) AS floor_Value_of_10.9;
输出:
floor_Value_of_10.9
---
10
示例3: 此示例返回1.1的底部值:
SELECT floor(1.1) AS floor_Value_of_1.1;
输出:
floor_Value_of_1.1
---
1
示例 4: 此示例返回21到30之间所有数字的floor值:
SELECT FLOOR(21.8) AS 21.8, FLOOR(22.9) AS 22.9, FLOOR(23.58) AS 23.58, FLOOR(24.55) AS 24.55, FLOOR(25.05) AS 25.05, FLOOR(26) AS 26, FLOOR(27.125) AS 27.125, FLOOR(28.98) AS 28.98, FLOOR(29.89) AS 29.89, FLOOR(30.02) AS 30.02;
输出:
21.8 | 22.9 | 23.58 | 24.55 | 25.05 | 26 | 27.125 | 28.98 | 29.89 | 30.02 |
---|---|---|---|---|---|---|---|---|---|
21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
示例5: 此示例使用SQL表格中的floor函数。
在此示例中,我们将通过新表格来执行floor函数对表格的列进行操作:
下面展示了在SQL中创建新表格的语法:
CREATE TABLE Name_of_New_Table
(
First_Column_of_table Data Type (character_size of First Column),
Second_Column_of_table Data Type (character_size of the Second column ),
Third_Column_of_table Data Type (character_size of the Third column),
??.,
Last_Column_of_table Data Type (character_size of the Last column)
);
以下CREATE语句创建 Product_Details 表,用于存储产品的价格和数量:
CREATE TABLE Product_Details
(
Product_ID INT NOT NULL,
Product_Name Varchar(50),
Product_Quantity INT,
Purchasing_Price INT,
Selling_Price INT,
Release_Date Date,
Product_Rating INT
);
以下多个INSERT查询将产品的记录以及其销售和购买价格插入到Product_Details表中:
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (0.1, P1, 36.250, 15.5, NULL, 2022-04-30, NULL);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (0.2, P4, 75.500, 2.45, 14.8, 2022-01-28, 9.25);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (0.3, P2, 68.350, 95.85, 12.250, 2022-02-18, 9.15);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (0.4, P7, 48.850, 85.355, NULL, 2021-12-25, 8.45);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (1.5, P6, 35.900, 0.5, 0.500, 2021-10-15, NULL);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (2.6, P8, 20.750, 112.110, 9.95, 2022-01-28, 9.9);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (0.7, P10, 12.250, 999.550, 0.258, 2022-04-11, NULL);
下面的SELECT语句显示了上述 Product_Details 表中插入的记录:
SELECT * FROM Product_Details;
Product_ID | Product_Name | Product_Quantity | Purchasing_Price | Selling_Price | Release_Date | Product_Rating |
---|---|---|---|---|---|---|
0.1 | P1 | 36.250 | 15.5 | NULL | 2022-04-30 | NULL |
0.2 | P4 | 75.500 | 2.45 | 14.8 | 2022-01-28 | 9.25 |
0.3 | P2 | 68.350 | 95.85 | 12.250 | 2022-02-18 | 9.15 |
0.4 | P7 | 48.850 | 85.355 | NULL | 2021-12-25 | 8.45 |
1.5 | P6 | 35.900 | 0.5 | 0.500 | 2021-10-15 | NULL |
2.6 | P8 | 20.750 | 112.110 | 9.95 | 2022-01-28 | 9.9 |
0.7 | P10 | 12.250 | 999.550 | 0.258 | 2022-04-11 | NULL |
查询1: 在上述Product_Details表的Product_ID列上使用floor函数的以下SELECT查询:
SELECT Product_ID, FLOOR(Product_ID) AS floor_Value_of_Product_ID FROM Product_Details;
此查询显示每个产品的产品ID的floor值。
输出:
Product_ID | floor_Value_of_Product_ID |
---|---|
0.1 | 0 |
0.2 | 0 |
0.3 | 0 |
0.4 | 0 |
1.5 | 1 |
2.6 | 2 |
0.7 | 0 |
查询2: 以下SELECT查询使用上述Product_Details表中Purchasing_Price和Selling_Price列的floor函数:
SELECT Purchasing_Price, FLOOR(Purchasing_Price) AS floor_Value_of_PurchasingPrice, Selling_Price, FLOOR(Selling_Price) AS floor_Value_of_SellingPrice FROM Product_Details;
该查询显示了上表中每个产品的采购价和销售价的floor值。
输出:
Purchasing_Price | floor_Value_of_PurchasingPrice | Selling_Price | floor_Value_of_SellingPrice |
---|---|---|---|
15.5 | 15 | NULL | - |
2.45 | 2 | 14.8 | 14 |
95.85 | 95 | 12.250 | 12 |
85.355 | 85 | NULL | - |
0.5 | 0 | 0.500 | 0 |
112.110 | 112 | 9.95 | 9 |
999.550 | 999 | 0.258 | 0 |
查询 3: 下面的 SELECT 查询使用了上述 Product_Details 表中的 Product_Rating 列的 floor 函数:
SELECT floor(Product_Rating) AS floor_Value_of_productrating FROM Product_Details;
该查询显示了上表中每个产品的评分的 floor 值。
输出:
Product_Rating | floor_Value_of_productrating |
---|---|
NULL | - |
9.25 | 9 |
9.15 | 9 |
8.45 | 8 |
NULL | - |
9.9 | 9 |
NULL | - |
查询4: 以下的SELECT查询在上述的Product_Details表中使用了floor函数和Product_Quantity列:
SELECT Product_Quantity, FLOOR(Product_Quantity) AS floor_Value_of_Product_Quantity FROM Product_Details;
这个查询展示了每个产品的产品数量的floor值。
输出:
Product_Quantity | floor_Value_of_Product_Quantity |
---|---|
36.250 | 36 |
75.500 | 75 |
68.350 | 68 |
48.850 | 48 |
35.900 | 35 |
20.750 | 20 |
12.250 | 12 |