SQL SIN函数
SIN是一个SQL数学函数,用于返回指定数字的正弦值。
SIN函数的语法
SELECT SIN(Number) AS Alias_Name;
在SIN语法中,我们必须传递要返回其正弦值的十进制数。
在结构化查询语言中,我们还可以使用SIN函数以表的字段为参数,如下所示:
SELECT SIN(column_Name) AS Alias_Name FROM Table_Name;
在这个语法中,我们需要定义我们想要执行SIN函数的表的名称和列。
SIN函数的示例
示例1: 这个示例返回给定查询中这个数字的正弦值:
SELECT SIN(101) AS Sin_Value_of_101;
输出:
Sin_Value_of_101
---
0.45202578717835057
示例 2: 此示例从以下SELECT查询中返回2的正弦值:
SELECT SIN(2) AS Sin_Value_of_2;
输出:
Sin_Value_of_2
---
100.90929742682568171
示例3: 此示例返回8的正弦值:
SELECT SIN(8) AS Sin_Value_of_8;
输出:
Sin_Value_of_8
---
0.98935824662338179
示例 4: 这个示例返回的 Sin_Value 是255:
SELECT SIN(255) AS Sin_Value_of_255;
输出:
Sin_Value_of_255
---
-0.50639163492449091
示例 5: 此示例返回NULL的Sin_Value:
SELECT SIN(NULL) AS Sin_Value_of_NULL;
输出:
Sin_Value_of_NULL
---
\-
示例6: 此示例返回21到30之间所有数字的正弦值:
SELECT SIN(21) AS 21, SIN(22) AS 22, SIN(23) AS 23, SIN(24) AS 24, SIN(25) AS 25, SIN(26) AS 26, SIN(27) AS 27, SIN(28) AS 28, SIN(29) AS 29, SIN(30) AS 30;
输出:
21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
---|---|---|---|---|---|---|---|---|---|
0.8366 | -8.8513 | -0.846 | -0.9055 | -0.1323 | 0.7625 | 0.9563 | 0.2709 | 0.6636 | -0.988 |
示例7: 此示例使用带有SQL表的SIN函数。
在这个示例中,我们将创建一个新表,通过该表我们将对数字列执行SIN函数:
以下显示了在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, 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.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, 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.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, 15.500, 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.750, 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.250, 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.250 | 945 | NULL | 2022-04-30 | NULL |
202 | P4 | 15.500 | 45 | 75 | 2022-01-28 | 5 |
103 | P2 | 18.250 | 25 | NULL | 2022-02-18 | 4 |
111 | P7 | 25.250 | 5 | 15 | 2021-12-25 | 9 |
210 | P6 | 15.500 | 50 | 70 | 2021-10-15 | NULL |
212 | P8 | 19.750 | 110 | 250 | 2022-01-28 | 4 |
112 | P10 | 10.250 | 550 | 835 | 2022-04-11 | NULL |
查询 1: 以下 SELECT 查询使用了上述 Product_Details 表的 Product_Quantity 列的 SIN 函数:
SELECT Product_ID, SIN(Product_ID) AS Sin_Value_of_Product_ID FROM Product_Details;
这个查询返回表中每个产品id的正弦值。
输出:
Product_ID | Sin_Value_of_Product_ID |
---|---|
104 | -0.3216 |
202 | 0.80641 |
103 | 0.622 |
111 | -0.8654 |
210 | 0.4677185 |
212 | -0.99834 |
112 | -0.8899956 |
查询2: 以下SELECT查询使用了SIN函数,与上述Product_Details表的Purchasing_Price和Selling_Price列一起使用:
SELECT Purchasing_Price, SIN(Purchasing_Price) AS Sin_Value_of_PurchasingPrice, Selling_Price, SIN(Selling_Price) AS Sin_Value_of_SellingPrice FROM Product_Details;
这个查询显示了每个产品的购买和销售价格的正弦值。
输出:
Purchasing_Price | Sin_Value_of_PurchasingPrice | Selling_Price | Sin_Value_of_SellingPrice |
---|---|---|---|
945 | 0.58053 | NULL | |
45 | 0.85090 | 75 | -0.387781 |
25 | -0.132351 | NULL | |
5 | -0.9589 | 15 | -0.650287 |
50 | -0.262374 | 70 | -0.773890 |
110 | -4.42426 | 250 | -0.97052 |
550 | -0.21948 | 835 | -0.61599 |
查询3: 以下SELECT查询使用SIN函数与上述Product_Details表的Product_Rating列:
SELECT SIN(Product_Rating) AS Sin_Value_of_productrating FROM Product_Details;
这个查询显示了上表中每个产品的评分的正弦值。
输出:
Product_Rating | Sin_Value_of_productrating |
---|---|
NULL | |
5 | -0.9589 |
4 | -0.7568024 |
9 | 0.41211 |
NULL | |
4 | -0.7568024 |
NULL |