mysql分组随机取数据_MySql分组后随机获取每组⼀条数据的
操作
思路:先随机排序然后再分组就好了。
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 33 王五-1 1
4 张三-2 2
7 张三-3 32 李四-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 '源⽹元端⼝',
mysql删除重复的数据保留一条`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_aming_type,t1.interface,t1.xdr_id,t1.rat,t1.imsi,t1.i
' into
@id,@length,@local_province,@local_city,@owner_province,@owner_city,@roaming_type,@interface,@xdr_id,@rat,@imsi,@imei,@ms ',
' 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,pro
values(@id,@length,@local_province,@local_city,@owner_province,@owner_city,@roaming_type,@interface,@xdr_id,@rat,@imsi,
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_tim from xdr_authen_tmp;
end;
truncate table xdr_authen_tmp;
end
;;
delimiter ;
以上这篇mysql分组后随机获取每组⼀条数据的操作就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持萬仟
⽹。
希望与⼴⼤⽹友互动??
点此进⾏留⾔吧!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。