欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySql分组后随机获取每组一条数据的操作

程序员文章站 2022-06-22 08:58:47
思路:先随机排序然后再分组就好了。1、创建表:create table `xdx_test` ( `id` int(11) not null, `name` varchar(255) default...

思路:先随机排序然后再分组就好了。

1、创建表:

create table `xdx_test` (
 `id` int(11) not null,
 `name` varchar(255) default null,
 `class` varchar(255) default null,
 primary key (`id`)
) engine=innodb default charset=utf8mb4;

2、插入数据

insert into xdx_test values (1, '张三-1','1');
insert into xdx_test values (2, '李四-1','1');
insert into xdx_test values (3, '王五-1','1');
insert into xdx_test values (4, '张三-2','2');
insert into xdx_test values (5, '李四-2','2');
insert into xdx_test values (6, '王五-2','2');
insert into xdx_test values (7, '张三-3','3');
insert into xdx_test values (8, '李四-3','3');
insert into xdx_test values (9, '王五-3','3');

3、查询语句

select * from 
 (select * from xdx_test order by rand()) a
group by a.class

4、查询结果

3 王五-1 1

5 李四-2 2

9 王五-3 3

3 王五-1 1

4 张三-2 2

7 张三-3 3

2 李四-1 1

5 李四-2 2

8 李四-3 3

补充知识:mysql实现随机获取几条数据的方法(效率和离散型比较)

sql语句有几种写法、效率、以及离散型 比较

1:select * from tablename order by rand() limit 想要获取的数据条数;

2:select *from `table` where id >= (select floor( max(id) * rand()) from `table` ) order by id limit 想要获取的数据条数;

3:select * from `table` as t1 join (select round(rand() * (select max(id) from `table`)) as id) as t2 where t1.id >= t2.id

order by t1.id asc limit 想要获取的数据条数;

4:select * from `table`where id >= (select floor(rand() * (select max(id) from `table`))) order by id limit 想要获取的数据条数;

5:select * from `table` where id >= (select floor( rand() * ((select max(id) from `table`)-(select min(id) from `table`)) + (select min(id) from `table`))) order by id limit 想要获取的数据条数;

6:select * from `table` as t1 join (select round(rand() * ((select max(id) from `table`)-(select min(id) from `table`))+(select min(id) from `table`)) as id) as t2 where t1.id >= t2.id order by t1.id limit 想要获取的数据条数;

1的查询时间>>2的查询时间>>5的查询时间>6的查询时间>4的查询时间>3的查询时间,也就是3的效率最高。

以上6种只是单纯的从效率上做了比较;

上面的6种随机数抽取可分为2类:

第一个的离散型比较高,但是效率低;其他5个都效率比较高,但是存在离散性不高的问题;

怎么解决效率和离散型都满足条件啦?

我们有一个思路就是: 写一个存储过程;

select * from test t1 join (select round(rand() * ((select max(id) from test)-(select min(id) from test)) + (select min(id) from test)) as id) t2 where t1.id >= t2.id limit 1

每次取出一条,然后循环写入一张临时表中;最后返回 select 临时表就ok;

这样既满足了效率又解决了离散型的问题;可以兼并二者的优点;

下面是具体存储过程的伪代码

drop procedure if exists `evaluate_check_procedure`;
delimiter ;;
create definer=`root`@`%` procedure `evaluate_check_procedure`(in starttime datetime, in endtime datetime,in checknum int,in evainterface varchar(36))
begin

-- 新建一张临时表 ,存放随机取出的数据

create temporary table if not exists xdr_authen_tmp ( 
 `id` bigint(20) not null auto_increment comment '序号',
 `length` int(5) default null comment '字节数',
 `interface` int(3) not null comment '接口',
 `xdr_id` varchar(32) not null comment 'xdr id',
 `msisdn` varchar(32) default null comment '用户号码',
 `procedure_start_time` datetime not null default '0000-00-00 00:00:00' comment '开始时间',
 `procedure_end_time` datetime default null comment '结束时间',
 `source_ne_ip` varchar(39) default null comment '源网元ip',
 `source_ne_port` int(5) default null comment '源网元端口',
 `destination_ne_ip` varchar(39) default null comment '目的网元ip',
 `destination_ne_port` int(5) default null comment '目的网元端口',
 `insert_date` datetime default null comment '插入时间',
 `extend1` varchar(50) default null comment '扩展1',
 `extend2` varchar(50) default null comment '扩展2',
 `extend3` varchar(50) default null comment '扩展3',
 `extend4` varchar(50) default null comment '扩展4',
 `extend5` varchar(50) default null comment '扩展5',
 primary key (`id`,`procedure_start_time`),
 key `index_procedure_start_time` (`procedure_start_time`),
 key `index_source_dest_ip` (`source_ne_ip`,`destination_ne_ip`),
 key `index_xdr_id` (`xdr_id`) 
) engine = innodb default charset=utf8;

begin
declare j int;
declare i int;

declare continue handler for not found set i = 1;

-- 这里的checknum是需要随机获取的数据数,比如随机获取10条,那这里就是10,通过while循环来逐个获取单个随机记录;

set j = 0;
while j < checknum do 
 set @sqlexi = concat( ' select t1.id,t1.length,t1.local_province,t1.local_city,t1.owner_province,t1.owner_city,t1.roaming_type,t1.interface,t1.xdr_id,t1.rat,t1.imsi,t1.imei,t1.msisdn,t1.procedure_start_time,t1.procedure_end_time,t1.transaction_type,t1.transaction_status,t1.source_ne_ip,t1.source_ne_port,t1.destination_ne_ip,t1.destination_ne_port,t1.result_code,t1.experimental_result_code,t1.origin_realm,t1.destination_realm,t1.origin_host,t1.destination_host,t1.insert_date',
    ' into @id,@length,@local_province,@local_city,@owner_province,@owner_city,@roaming_type,@interface,@xdr_id,@rat,@imsi,@imei,@msisdn,@procedure_start_time,@procedure_end_time,@transaction_type,@transaction_status,@source_ne_ip,@source_ne_port,@destination_ne_ip,@destination_ne_port,@result_code,@experimental_result_code,@origin_realm,@destination_realm,@origin_host,@destination_host,@insert_date ',
    ' from xdr_authen t1 join (select round(rand() * ((select max(id) from xdr_authen)-(select min(id) from xdr_authen)) + (select min(id) from xdr_authen)) as id) t2',
    ' where t1.procedure_start_time >= "',starttime,'"',
       ' and t1.procedure_start_time < "',endtime,'"',' and t1.interface in (',evainterface,')',
       ' and t1.id >= t2.id limit 1');
 prepare sqlexi from @sqlexi;
 execute sqlexi;
 deallocate prepare sqlexi;

-- 这里获取的记录有可能会重复,如果是重复数据,我们则不往临时表中插入此条数据,再进行下一次随机数据的获取。依次类推,直到随机数据取够为止;

 select count(1) into @num from xdr_authen_tmp where id = @id;
 
 if @num > 0 or i=1 then 
  set j = j;
 else
  insert into xdr_authen_tmp(id,length,local_province,local_city,owner_province,owner_city,roaming_type,interface,xdr_id,rat,imsi,imei,msisdn,procedure_start_time,procedure_end_time,transaction_type,transaction_status,source_ne_ip,source_ne_port,destination_ne_ip,destination_ne_port,result_code,experimental_result_code,origin_realm,destination_realm,origin_host,destination_host,insert_date)
  values(@id,@length,@local_province,@local_city,@owner_province,@owner_city,@roaming_type,@interface,@xdr_id,@rat,@imsi,@imei,@msisdn,@procedure_start_time,@procedure_end_time,@transaction_type,@transaction_status,@source_ne_ip,@source_ne_port,@destination_ne_ip,@destination_ne_port,@result_code,@experimental_result_code,@origin_realm,@destination_realm,@origin_host,@destination_host,@insert_date);
 
  set j = j + 1;
 end if; 
 set i=0;

end while; 

-- 最后我们将所有的随机数查询出来,以结果集的形式返回给后台

select id,length,local_province,local_city,owner_province,owner_city,roaming_type,interface,xdr_id,rat,imsi,imei,msisdn,procedure_start_time,procedure_end_time,transaction_type,transaction_status,source_ne_ip,source_ne_port,destination_ne_ip,destination_ne_port,result_code,experimental_result_code,origin_realm,destination_realm,origin_host,destination_host,insert_date from xdr_authen_tmp;

end;
truncate table xdr_authen_tmp;

end
;;
delimiter ;

以上这篇mysql分组后随机获取每组一条数据的操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。