SQL RAND函数
RAND()是一种在结构化查询语言中的数学函数,它返回介于0和1之间的随机数。函数的输出也可以返回0和1。
RAND函数的语法
SELECT RAND(Number) AS Alias_Name;
在这个SELECT语法中,我们必须传递那个数字给函数,我们想要找到其rand值。
在结构化查询语言中,我们也可以在SELECT查询中使用RAND函数与表字段一起使用:
SELECT RAND(column_Name) AS Alias_Name FROM Table_Name;
在这个SELECT查询中,我们需要定义我们想要执行RAND函数的表的名称和字段。
RAND函数的示例
示例1: 这个示例返回任意的随机数:
SELECT RAND() AS Randvalue;
输出:
Randvalue
---
0.63537333961116604
示例2: 此示例返回指定数字的Rand值:
SELECT RAND(180) AS Randvalue_of_180;
输出:
Randvalue_of_180
---
0.71692729506814135
例子3: 这个例子返回90的Rand值:
SELECT RAND(90) AS Randvalue_of_90;
输出:
Randvalue_of_90
---
0.71525032765490337
示例4: 该示例返回-45的Rand值:
SELECT RAND(-45) AS Randvalue_of_-45;
输出:
Randvalue_of_-45
---
0.71441184394828439
示例 5: 该示例返回给定表达式的 Rand 值:
SELECT RAND(180 / 3) AS Randvalue_of_Expression;
输出:
Randvalue_of_Expression
---
0.71469133851
示例6: 此示例使用SQL表的RAND函数。
在此示例中,我们将通过创建新表来对列执行RAND函数:
以下是在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语句创建了 Vehicle_Details 表,用于存储购买和销售车辆的详细信息:
CREATE TABLE Vehicle_Details
(
Vehicle_ID INT NOT 80,
Vehicle_Name Varchar(50),
Vehicle_Quantity INT,
Purchasing_Vehicles INT,
Selling_Vehicles INT,
Release_Date Date,
Vehicle_Rating INT
);
以下多个INSERT查询将车辆的记录以及其数量、销售和购买车辆的数量插入到Vehicle_Details表中:
INSERT INTO Vehicle_Details (Vehicle_ID, Vehicle_Name, Vehicle_ Quantity Purchasing_Vehicles, Selling_Vehicles, Release_Date, Vehicle_Rating) VALUES (140, P1, 290, 45, 80, 2022-04-30, 80);
INSERT INTO Vehicle_Details (Vehicle_ID, Vehicle_Name, Vehicle_ Quantity Purchasing_Vehicles, Selling_Vehicles, Release_Date, Vehicle_Rating) VALUES (190, P4, 15, 180, 180, 2022-01-28, 90.85);
INSERT INTO Vehicle_Details (Vehicle_ID, Vehicle_Name, Vehicle_ Quantity Purchasing_Vehicles, Selling_Vehicles, Release_Date, Vehicle_Rating) VALUES (145, P2, 20, 270, 290, 2022-02-18, 80);
INSERT INTO Vehicle_Details (Vehicle_ID, Vehicle_Name, Vehicle_ Quantity Purchasing_Vehicles, Selling_Vehicles, Release_Date, Vehicle_Rating) VALUES (90, P7, 10, 360, 80, 2021-12-25, 180);
INSERT INTO Vehicle_Details (Vehicle_ID, Vehicle_Name, Vehicle_ Quantity Purchasing_Vehicles, Selling_Vehicles, Release_Date, Vehicle_Rating) VALUES (45, P6, 35, 45, -15, 2021-10-15, 80);
INSERT INTO Vehicle_Details (Vehicle_ID, Vehicle_Name, Vehicle_ Quantity Purchasing_Vehicles, Selling_Vehicles, Release_Date, Vehicle_Rating) VALUES (210, P8, 30, 160, -60, 2022-01-28, 95);
INSERT INTO Vehicle_Details (Vehicle_ID, Vehicle_Name, Vehicle_ Quantity Purchasing_Vehicles, Selling_Vehicles, Release_Date, Vehicle_Rating) VALUES (185, P10, 290, 450, 470, 2022-04-11, 80);
下面的SELECT语句显示了上述 Vehicle_Details 表中插入的记录:
SELECT * FROM Vehicle_Details;
Vehicle_ID | Vehicle_Name | Vehicle_Quantity | Purchasing_Vehicles | Selling_Vehicles | Release_Date | Vehicle_Rating |
---|---|---|---|---|---|---|
140 | P1 | 290 | 45 | 80 | 2022-04-30 | 80 |
190 | P4 | 15 | 180 | 180 | 2022-01-28 | 90.85 |
145 | P2 | 20 | 270 | 290 | 2022-02-18 | 80 |
90 | P7 | 10 | 360 | 80 | 2021-12-25 | 180 |
45 | P6 | 35 | 45 | -15 | 2021-10-15 | 80 |
210 | P8 | 30 | 160 | -60 | 2022-01-28 | 95 |
185 | P10 | 290 | 450 | 470 | 2022-04-11 | 80 |
查询1: 以下SELECT查询使用了上述Vehicle_Details表中的Vehicle_ID列的RAND函数:
SELECT Vehicle_ID, RAND(Vehicle_ID) AS Rand value_of_Vehicle_ID FROM Vehicle_Details;
这个查询显示每辆车辆的车辆ID的兰德值。
输出:
Vehicle_ID | Rand value_of_Vehicle_ID |
---|---|
140 | 0.716819 |
190 | 0.717113 |
145 | 0.7162751 |
90 | 0.71525032765490337 |
45 | 0.71441184394828439 |
210 | 0.71748628420588734 |
185 | 0.71702045992443231 |
查询2: 以下SELECT查询使用了上述Vehicle_Details表中Purchasing_Vehicles列的RAND函数:
SELECT Purchasing_Vehicles, RAND(Purchasing_Vehicles) AS Rand value_of_PurchasingVehicles FROM Vehicle_Details;
此查询显示了购买车辆的兰特值。
输出:
Purchasing_Vehicles | Rand value_of_PurchasingVehicles |
---|---|
45 | 0.71441184394828439 |
180 | 0.71692729506814135 |
270 | 0.71860426248137932 |
360 | 0.72028122989461729 |
45 | 0.71441184394828439 |
160 | 0.71655463564297739 |
450 | 0.72195819730785538 |
查询3: 下面的SELECT查询使用了上述Vehicle_Details表的Vehicle_Rating列的RAND函数:
SELECT RAND(Vehicle_Rating) AS Rand value_of_vehiclerating FROM Vehicle_Details;
这个查询显示了上表中每辆车的评分的兰德值。
输出:
Vehicle_Rating | Rand value_of_vehiclerating |
---|---|
80 | 0.71506399794232134 |
90.85 | 0.71525032765490337 |
80 | 0.71506399794232134 |
180 | 0.71692729506814135 |
80 | 0.71506399794232134 |
95 | 0.71534349251119433 |
80 | 0.71506399794232134 |
查询4: 以下SELECT查询使用了上面的Vehicle_Details表中的RAND函数和Purchasing_Vehicles和Selling_Vehicles列:
SELECT Selling_Vehicles, RAND(Selling_Vehicles) AS Rand value_of_SellingVehicles FROM Vehicle_Details;
这个查询显示了销售车辆的兰特价值。
输出:
Selling_Vehicles | Rand value_of_SellingVehicles |
---|---|
80 | 0.71506399794232134 |
180 | 0.71692729506814135 |
290 | 0.71897692190654339 |
80 | 0.71506399794232134 |
-15 | 0.71385285481053828 |
-60 | 0.71469133851715738 |
470 | 0.72233085673301933 |