SQL POWER函数
POWER是SQL中的数学函数,返回一个数的另一个数的幂值。在POWER函数中,我们需要将两个数作为参数传递,其中一个数作为指数,另一个数作为底数。
POWER字符串函数的语法
SELECT POWER(Number1, Number2) AS Alias_Name;
在这个POWER函数中,有以下两个参数:
- Number1: 它在幂函数中充当底数
- Number2: 它在幂函数中充当指数。
在结构化查询语言中,我们还可以使用POWER函数来操作表中的整数列,如下所示:
SELECT POWER(column_Name1, column_Name2) AS Alias_Name FROM Table_Name;
在这个语法中,我们使用了带有现有SQL表的POWER函数。在这里,我们必须定义我们想要执行POWER函数的那个表的名称和列。
POWER字符串函数的例子
例子1: 以下POWER函数返回基本值5和指数值3的结果:
SELECT POWER(5,3) AS POWER_of5;
输出:
POWER_of5
---
125
示例 2: 以下的POWER函数返回结果为625:
SELECT POWER(25, 2) AS POWER_of25;
输出:
POWER_of25
---
625
示例3: 此示例使用具有SQL表格的数学POWER函数: 在第三个示例中,我们将创建新表格,通过该表格我们将对表格的整数列进行POWER函数操作:
下面显示了在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.250, 945, 1050, 2022-04-30, 8);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (202, P4, 15.500, 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.250, 25, 475, 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.250, 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, 12.650, 50, 70, 2021-10-15, 1);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (212, P8, 19.750, 110, 250, 2022-01-10, 3);
INSERT INTO Product_Details (Product_ID, Product_Name, Product_ Quantity Purchasing_Price, Selling_Price, Release_Date, Product_Rating) VALUES (112, P10, 24.950, 550, 835, 2022-04-11, 8);
下面的SELECT语句显示了以上 Product_Details 表中插入的记录:
SELECT * FROM Product_Details;
Product_ID | Product_Name | Product_Quantity | Purchasing_Price | Selling_Price | Release_Date | Product_Rating |
---|---|---|---|---|---|---|
104 | P1 | 10.250 | 945 | 1050 | 2022-04-30 | 8 |
202 | P4 | 15.500 | 45 | 75 | 2022-01-28 | 5 |
103 | P2 | 18.250 | 25 | 475 | 2022-02-18 | 4 |
111 | P7 | 25.250 | 5 | 15 | 2021-12-25 | 9 |
210 | P6 | 12.650 | 50 | 70 | 2021-10-15 | 1 |
212 | P8 | 19.750 | 110 | 250 | 2022-01-10 | 3 |
112 | P10 | 24.950 | 550 | 835 | 2022-04-11 | 8 |
查询1: 下面的SELECT查询使用了上述Product_Details表中的Product_Quantity和Power_ID列的POWER函数:
SELECT Product_ID POWER(Product_Quantity, 2) AS POWER2_of_ID, Product_Quantity, POWER(Product_Quantity, 1) AS POWER1_ofquantity FROM Product_Details;
输出:
Product_ID | POWER2_of_ID | Product_Quantity | POWER1_ofquantity |
---|---|---|---|
104 | 10816 | 10.250 | 10.250 |
202 | 40804 | 15.500 | 15.500 |
103 | 10609 | 18.250 | 18.250 |
111 | 12321 | 25.250 | 25.250 |
210 | 44100 | 12.650 | 12.650 |
212 | 44944 | 19.750 | 19.750 |
112 | 12544 | 24.950 | 24.950 |
查询 2: 下面的 SELECT 查询在上述 Product_Details 表中,使用 Purchasing_Price 和 Selling_Price 列的 POWER 函数,并且 Product_ID 大于 103 的产品:
SELECT Product_ID, Purchasing_Price, POWER(Purchasing_Price, 2) AS POWER2_of_purchase, Selling_Price, POWER( Selling_Price, 2) AS POWER2_of_selling FROM Product_Details WHERE Product_ID > 103;
输出:
Product_ID | Purchasing_Price | POWER2_of_purchase | Selling_Price | POWER2_of_selling |
---|---|---|---|---|
104 | 945 | 893025 | 1050 | 1102500 |
202 | 45 | 2025 | 75 | 5625 |
111 | 5 | 25 | 15 | 225 |
210 | 50 | 2500 | 70 | 4900 |
212 | 110 | 12100 | 250 | 62500 |
112 | 550 | 302500 | 835 | 697225 |
查询3: 下面的SELECT查询使用了上面Product_Details表的Product_Rating列的POWER函数:
SELECT Product_Rating, POWER(Product_Rating, 3) AS POWER3_of_rating FROM Product_Details;
输出:
Product_Rating | POWER3_of_rating |
---|---|
8 | 512 |
5 | 125 |
4 | 64 |
9 | 729 |
1 | 1 |
3 | 27 |
8 | 512 |