使用 SQL 语句实现一个年会抽奖程序的代码

年关将近,抽奖想必是大家在公司年会上最期待的活动了。如果老板让你做一个年会抽奖的程序,你会怎么实现呢?今天给大家介绍一下如何通过 sql 语句来实现这个功能。实现的原理其实非常简单,就是通过函数为每个人分配一个随机数,然后取最大或者最小的 n 个随机数对应的员工。

本文使用的示例表。

oracle

oracle 提供了一个系统程序包dbms_random,可以用于生成随机数据,包括随机数字和随机字符串等。其中,dbms_random.value 函数可以用于生成一个大于等于 0 小于 1 的随机数字。利用这个函数,我们可以从表中返回随机的数据行。例如:

select emp_id, emp_name
from employee 
order by dbms_random.value
fetch first 1 rows only;

emp_id|emp_name|
------|--------|
 3|张飞 |

再次执行以上查询将会返回其他员工。我们也可以一次返回多名随机员工:

select emp_id, emp_name
from employee 
order by dbms_random.value
fetch first 3 rows only;

emp_id|emp_name|
------|--------|
 6|魏延 |
 21|黄权 |
 9|赵云 |

为了避免同一个员工中奖多次,可以创建一个存储已中奖员工的表:

每次开奖时

-- 中奖员工表
create table emp_win(
 emp_id integer primary key, -- 员工编号
 emp_name varchar(50) not null, -- 员工姓名
 grade varchar(50) not null -- 中奖级别
);

将中奖员工和级别存入 emp_win 表中,同时每次开奖时排除已经中奖的员工。例如,以下语句可以抽出 3 名三等奖:

insert into emp_win
select emp_id, emp_name, '三等奖'
from employee
where emp_id not in (select emp_id from emp_win) -- 排除已经中奖的员工
order by dbms_random.value
fetch first 3 rows only;

select * from emp_win;

emp_id|emp_name|grade |
------|--------|--------|
 8|孙丫鬟 |三等奖 |
 3|张飞 |三等奖 |
 9|赵云 |三等奖 |

继续抽出 2 名二等奖和 1 名一等奖:

-- 二等奖2名
insert into emp_win
select emp_id, emp_name, '二等奖'
from employee
where emp_id not in (select emp_id from emp_win)
order by dbms_random.value
fetch first 2 rows only;

-- 一等奖1名
insert into emp_win
select emp_id, emp_name, '一等奖'
from employee
where emp_id not in (select emp_id from emp_win)
order by dbms_random.value
fetch first 1 rows only;

select * from emp_win;

emp_id|emp_name|grade |
------|--------|-------|
 8|孙丫鬟 |三等奖 |
 3|张飞 |三等奖 |
 9|赵云 |三等奖 |
 6|魏延 |二等奖 |
 22|糜竺 |二等奖 |
 10|廖化 |一等奖 |

我们可以进一步将以上语句封装成一个存储过程:

create or replace procedure luck_draw(pv_grade varchar, pn_num integer)
is
begin
	insert into emp_win
 select emp_id, emp_name, pv_grade
 from employee
 where emp_id not in (select emp_id from emp_win)
 order by dbms_random.value
 fetch first pn_num rows only;

 commit;
end luck_draw;
/

call luck_draw('特等奖', 1);

select * from emp_win where grade = '特等奖';

emp_id|emp_name|grade |
------|--------|-------|
 25|孙乾 |特等奖 |

关于 oracle 中如何生成随机数字、字符串、日期、验证码以及 uuid,可以

mysql

mysql 提供了一个系统函数rand,可以用于生成一个大于等于 0 小于 1 的随机数字。利用这个函数,我们可以从表中返回随机记录。例如:

select emp_id, emp_name
from employee 
order by rand()
limit 1;

emp_id|emp_name|
------|--------|
 19|庞统 |

再次执行以上语句将会返回其他员工。我们也可以一次返回多名随机的员工:

select emp_id, emp_name
from employee 
order by rand()
limit 3;

emp_id|emp_name|
------|--------|
 1|刘备 |
 20|蒋琬 |
 23|邓芝 |

为了避免同一个员工中奖多次,我们可以创建一个存储已中奖员工的表:

-- 中奖员工表
create table emp_win(
 emp_id integer primary key, -- 员工编号
 emp_name varchar(50) not null, -- 员工姓名
 grade varchar(50) not null -- 中奖级别
);

每次开奖时将中奖员工和级别存入 emp_win 表中,同时每次开奖时排除已经中奖的员工。例如,以下语句可以抽出 3 名三等奖:

insert into emp_win
select emp_id, emp_name, '三等奖'
from employee
where emp_id not in (select emp_id from emp_win) -- 排除已经中奖的员工
order by rand()
limit 3;

select * from emp_win;

emp_id|emp_name|grade |
------|--------|-------|
 18|法正 |三等奖 |
 23|邓芝 |三等奖 |
 24|简雍 |三等奖 |

我们继续抽出 2 名二等奖和 1 名一等奖:

-- 二等奖2名
insert into emp_win
select emp_id, emp_name, '二等奖'
from employee
where emp_id not in (select emp_id from emp_win) -- 排除已经中奖的员工
order by rand()
limit 2;

-- 一等奖1名
insert into emp_win
select emp_id, emp_name, '一等奖'
from employee
where emp_id not in (select emp_id from emp_win) -- 排除已经中奖的员工
order by rand()
limit 1;

select * from emp_win;

emp_id|emp_name|grade |
------|--------|-------|
 2|关羽 |二等奖 |
 18|法正 |三等奖 |
 20|蒋琬 |一等奖 |
 23|邓芝 |三等奖 |
 24|简雍 |三等奖 |
 25|孙乾 |二等奖 |

我们可以进一步将以上语句封装成一个存储过程:

delimiter $$

create procedure luck_draw(in pv_grade varchar(50), in pn_num integer)
begin
	insert into emp_win
 select emp_id, emp_name, pv_grade
 from employee
 where emp_id not in (select emp_id from emp_win)
 order by rand()
 limit pn_num;

 select * from emp_win;
end$$

delimiter ;

call luck_draw('特等奖', 1);

emp_id|emp_name|grade |
------|--------|-------|
 2|关羽 |二等奖 |
 8|孙丫鬟 |特等奖 |
 18|法正 |三等奖 |
 20|蒋琬 |一等奖 |
 23|邓芝 |三等奖 |
 24|简雍 |三等奖 |
 25|孙乾 |二等奖 |

关于 mysql 中如何生成随机数字、字符串、日期、验证码以及 uuid,可以。

microsoft sql server

microsoft sql server 提供了一个系统函数newid,可以用于生成一个随机的 guid。利用这个函数,我们可以从表中返回随机的数据行。例如:

select top(1) emp_id, emp_name
from employee 
order by newid();

emp_id|emp_name|
------|--------|
 25|孙乾 |

再次执行以上语句将会返回其他员工。我们也可以一次返回多名随机员工:

select top(3) emp_id, emp_name
from employee 
order by newid();

emp_id|emp_name|
------|--------|
 23|邓芝 |
 1|刘备 |
 21|黄权 |

虽然 microsoft sql server 提供了一个返回随机数字的 rand 函数,但是该函数对于所有的数据行都返回相同的结果,因此不能用于返回表中的随机记录。例如:

select top(3) emp_id, emp_name, rand() as rd
from employee 
order by rand();

emp_id|emp_name|rd |
------|--------|------------------|
 23|邓芝 |0.8623555267583647|
 18|法正 |0.8623555267583647|
 11|关平 |0.8623555267583647|

为了避免同一个员工中奖多次,我们可以创建一个存储已中奖员工的表:

-- 中奖员工表
create table emp_win(
 emp_id integer primary key, -- 员工编号
 emp_name varchar(50) not null, -- 员工姓名
 grade varchar(50) not null -- 中奖级别
);

我们在每次开奖时将中奖员工和级别存入 emp_win 表中,同时每次开奖时排除已经中奖的员工。例如,以下语句可以抽出 3 名三等奖:

insert into emp_win
select top(3) emp_id, emp_name, '三等奖'
from employee
where emp_id not in (select emp_id from emp_win) -- 排除已经中奖的员工
order by newid();

select * from emp_win;

emp_id|emp_name|grade|
------|--------|-----|
 14|张苞 |三等奖|
 17|马岱 |三等奖|
 21|黄权 |三等奖|

继续抽出 2 名二等奖和 1 名一等奖:

-- 二等奖2名
insert into emp_win
select top(2) emp_id, emp_name, '二等奖'
from employee
where emp_id not in (select emp_id from emp_win)
order by newid();

-- 一等奖1名
insert into emp_win
select top(1) emp_id, emp_name, '一等奖'
from employee
where emp_id not in (select emp_id from emp_win)
order by newid();

select * from emp_win;

emp_id|emp_name|grade|
------|--------|-----|
 14|张苞 |三等奖|
 15|赵统 |一等奖|
 17|马岱 |三等奖|
 18|法正 |二等奖|
 21|黄权 |三等奖|
 22|糜竺 |二等奖|

我们可以进一步将以上语句封装成一个存储过程:

create or alter procedure luck_draw(@pv_grade varchar(50), @pn_num integer)
as
begin
	insert into emp_win
 select top(@pn_num) emp_id, emp_name, @pv_grade
 from employee
 where emp_id not in (select emp_id from emp_win)
 order by newid()
 
 select * from emp_win
end;

exec luck_draw '特等奖', 1;

emp_id|emp_name|grade|
------|--------|-----|
 14|张苞 |三等奖|
 15|赵统 |一等奖|
 17|马岱 |三等奖|
 18|法正 |二等奖|
 21|黄权 |三等奖|
 22|糜竺 |二等奖|
 23|邓芝 |特等奖|

关于 microsoft sql server 中如何生成随机数字、字符串、日期、验证码以及 uuid,可以参考。

postgresql

postgresql 提供了一个系统函数 random,可以用于生成一个大于等于 0 小于 1 的随机数字。利用这个函数,我们可以从表中返回随机记录。例如:

select emp_id, emp_name
from employee 
order by random()
limit 1;

emp_id|emp_name|
------|--------|
 22|糜竺 |

再次执行以上语句将会返回其他员工。我们也可以一次返回多名随机的员工:

select emp_id, emp_name
from employee 
order by rand()
limit 3;

emp_id|emp_name|
------|--------|
 8|孙丫鬟 |
 4|诸葛亮 |
 9|赵云 |

为了避免同一个员工中奖多次,我们可以创建一个存储已中奖员工的表:

-- 中奖员工表
create table emp_win(
 emp_id integer primary key, -- 员工编号
 emp_name varchar(50) not null, -- 员工姓名
 grade varchar(50) not null -- 中奖级别
);

每次开奖时将中奖员工和级别存入 emp_win 表中,同时每次开奖时排除已经中奖的员工。例如,以下语句可以抽出 3 名三等奖:

insert into emp_win
select emp_id, emp_name, '三等奖'
from employee
where emp_id not in (select emp_id from emp_win) -- 排除已经中奖的员工
order by random()
limit 3;

select * from emp_win;

emp_id|emp_name|grade|
------|--------|-----|
 23|邓芝 |三等奖|
 15|赵统 |三等奖|
 24|简雍 |三等奖|

我们继续抽出 2 名二等奖和 1 名一等奖:

-- 二等奖2名
insert into emp_win
select emp_id, emp_name, '二等奖'
from employee
where emp_id not in (select emp_id from emp_win)
order by random()
limit 2;

-- 一等奖1名
insert into emp_win
select emp_id, emp_name, '一等奖'
from employee
where emp_id not in (select emp_id from emp_win)
order by random()
limit 1;

select * from emp_win;

emp_id|emp_name|grade|
------|--------|-----|
 23|邓芝 |三等奖|
 15|赵统 |三等奖|
 24|简雍 |三等奖|
 1|刘备 |二等奖|
 21|黄权 |二等奖|
 22|糜竺 |一等奖|

我们可以进一步将以上语句封装成一个存储过程:

create or replace procedure luck_draw(pv_grade in varchar, pn_num in integer)
language plpgsql
as $$
begin
	insert into emp_win
 select emp_id, emp_name, pv_grade
 from employee
 where emp_id not in (select emp_id from emp_win)
 order by random()
 limit pn_num;
end;
$$

call luck_draw('特等奖', 1);

select * from emp_win where grade = '特等奖';

emp_id|emp_name|grade|
------|--------|-----|
 5|黄忠 |特等奖|

关于 postgresql 中如何生成随机数字、字符串、日期、验证码以及 uuid,可以。

sqlite

sqlite 中的random 函数可以用于生成一个大于等于 -9223372036854775808 小于 9223372036854775807 的随机整数。利用这个函数,我们可以从表中返回随机的数据行。例如:

select emp_id, emp_name
from employee
order by random()
limit 1;

emp_id|emp_name|
------|--------|
 4|诸葛亮 |

再次执行以上语句将会返回其他员工。我们也可以一次返回多名随机员工:

select emp_id, emp_name
from employee
order by random()
limit 3;

emp_id|emp_name|
------|--------|
 16|周仓 |
 15|赵统 |
 11|关平 |

为了避免同一个员工中奖多次,我们可以创建一个存储已中奖员工的表:

-- 中奖员工表
create table emp_win(
 emp_id integer primary key, -- 员工编号
 emp_name varchar(50) not null, -- 员工姓名
 grade varchar(50) not null -- 中奖级别
);

我们在每次开奖时将中奖员工和级别存入 emp_win 表中,同时每次开奖时排除已经中奖的员工。例如,以下语句可以抽出 3 名三等奖:

insert into emp_win
select emp_id, emp_name, '三等奖'
from employee
where emp_id not in (select emp_id from emp_win) -- 排除已经中奖的员工
order by random()
limit 3;

select * from emp_win;

emp_id|emp_name|grade|
------|--------|-----|
 2|关羽 |三等奖|
 3|张飞 |三等奖|
 8|孙丫鬟 |三等奖|

继续抽出 2 名二等奖和 1 名一等奖:

-- 二等奖2名
insert into emp_win
select emp_id, emp_name, '二等奖'
from employee
where emp_id not in (select emp_id from emp_win)
order by random()
limit 2;

-- 一等奖1名
insert into emp_win
select emp_id, emp_name, '一等奖'
from employee
where emp_id not in (select emp_id from emp_win)
order by random()
limit 1;

select * from emp_win;

emp_id|emp_name|grade|
------|--------|-----|
 2|关羽 |三等奖|
 3|张飞 |三等奖|
 4|诸葛亮 |一等奖|
 8|孙丫鬟 |三等奖|
 16|周仓 |二等奖|
 23|邓芝 |二等奖|

关于 sqlite 中如何生成随机数字、字符串、日期、验证码以及 uuid,可以。

总结

我们通过数据库系统提供的随机数函数返回表中的随机记录,从而实现年会抽奖的功能。

到此这篇关于使用 sql 语句实现一个年会抽奖程序的文章就介绍到这了,更多相关sql年会抽奖程序内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐