SQL SQUARE函数
SQUARE是结构化查询语言中的数学函数,它返回指定数字的平方。此函数以浮点值形式显示结果。 我们可以在SQUARE函数中指定正数或负数,但它始终返回正值。
SQUARE字符串函数的语法
语法1: 此语法使用SQL表的列名来使用SQUARE函数:
SELECT SQUARE(Column_Name) AS Alias_Name FROM Table_Name;
在语法中,我们必须指定我们想要执行SQUARE字符串函数的那一列的名称。我们也可以在单个查询中使用多个square函数。
语法2: 此语法将SQUARE函数与数字一起使用:
SELECT SQUARE(Number);
SQUARE字符串函数的示例
示例1: 以下SELECT查询显示了4的平方:
SELECT SQUARE(4) AS SQUARE_of_4;
输出:
SQUARE_of_4
---
16.0
示例 2: 以下SELECT查询显示-20的平方:
SELECT SQUARE(-20) AS SQUARE_of_-20;
输出:
SQUARE_of_-20
---
400.0
示例 3: 下面的SELECT查询显示了6的平方:
SELECT SQUARE(6.2) AS SQUARE_6.2;
输出:
SQUARE_6.2
---
38.44
例子4: 下面的SELECT查询展示了给定字符串的平方的前5个字符
SELECT SQUARE( 5) AS SQUARE_5_characters;
输出:
25
示例5:此示例使用SQL中的SQUARE函数与表格。
在此示例中,我们将通过新表格执行SQUARE函数来创建新的表格:
以下展示了在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 (104, P1, 10, 945, NULL, 2022-04-30, NULL);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (202, P4, 15, 45, 75, 2022-01-28, 5);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (103, P2, 18, 25, NULL, 2022-02-18, 4);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (111, P7, 25, 5, 15, 2021-12-25, 9);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (210, P6, 15, 50, 70, 2021-10-15, NULL);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (212, P8, 19, 110, 250, 2022-01-28, 4);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (112, P10, 10, 550, 835, 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 |
---|---|---|---|---|---|---|
104 | P1 | 10 | 945 | NULL | 2022-04-30 | NULL |
202 | P4 | 15 | 45 | 75 | 2022-01-28 | 5 |
103 | P2 | 18 | 25 | NULL | 2022-02-18 | 4 |
111 | P7 | 25 | 5 | 15 | 2021-12-25 | 9 |
210 | P6 | 15 | 50 | 70 | 2021-10-15 | NULL |
212 | P8 | 19 | 110 | 250 | 2022-01-28 | 4 |
112 | P10 | 10 | 550 | 835 | 2022-04-11 | NULL |
查询 1: :下面的 SELECT 查询使用了 Product_Details 表中的 Product_Quantity 列的 SQUARE 函数:
SELECT Product_ID, SQUARE(Product_ID) AS Square_of_ProductID FROM Product_Details;
这个查询显示了结果表中每个产品ID的平方
输出结果:
Product_ID | Square_of_ProductID_ |
---|---|
104 | 10816.0 |
202 | 40804.0 |
103 | 10609.0 |
111 | 12321.0 |
210 | 44100.0 |
212 | 44944.0 |
112 | 12544.0 |
查询2: :下面的SELECT查询使用了上述Product_Details表中产品数量与购买价格和销售价格列的乘积与SQUARE函数:
SELECT Purchasing_Price, Product_Quantity, SQUARE(Purchasing_Price * Product_Quantity) AS Square_oftotalprice, Selling_Price, Product_Quantity, SQUARE(Selling_Price * Product_Quantity) AS Square_oftotalprice FROM Product_Details;
输出:
Purchasing_Price | Product_Quantity | Square_oftotalprice | Selling_Price | Product_Quantity | Square_oftotalprice |
---|---|---|---|---|---|
945 | 10 | 89302500.0 | NULL | 10 | - |
45 | 15 | 455625.0 | 75 | 15 | 1265625.0 |
25 | 18 | 202500.0 | NULL | 18 | - |
5 | 25 | 15625.0 | 15 | 25 | 140625.0 |
50 | 15 | 562500.0 | 70 | 15 | 140625.0 |
110 | 19 | 562500.0 | 250 | 19 | 1102500.0 |
550 | 10 | 30250000.0 | 835 | 10 | 69722500.0 |
查询3: 以下SELECT查询在上述Product_Details表的Product_Rating列中使用了SQUARE函数:
SELECT SQUARE(Product_Rating) AS Square_ofrating FROM Product_Details;
输出:
Product_Rating | Square_ofrating |
---|---|
NULL | - |
5 | 25.0 |
4 | 16.0 |
9 | 81.0 |
NULL | - |
4 | 16 |
NULL | - |