主頁(yè) > 知識(shí)庫(kù) > MySql分組后隨機(jī)獲取每組一條數(shù)據(jù)的操作

MySql分組后隨機(jī)獲取每組一條數(shù)據(jù)的操作

熱門(mén)標(biāo)簽:廣東人工電話機(jī)器人 信陽(yáng)穩(wěn)定外呼系統(tǒng)運(yùn)營(yíng)商 石家莊電商外呼系統(tǒng) 百度地圖圖標(biāo)標(biāo)注中心 日照旅游地圖標(biāo)注 湖南人工外呼系統(tǒng)多少錢(qián) 芒果電話機(jī)器人自動(dòng)化 申請(qǐng)外呼電話線路 南通自動(dòng)外呼系統(tǒng)軟件

思路:先隨機(jī)排序然后再分組就好了。

1、創(chuàng)建表:

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、插入數(shù)據(jù)

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、查詢語(yǔ)句

SELECT * FROM 
 (SELECT * FROM xdx_test ORDER BY RAND()) a
GROUP BY a.class

4、查詢結(jié)果

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

補(bǔ)充知識(shí):mysql實(shí)現(xiàn)隨機(jī)獲取幾條數(shù)據(jù)的方法(效率和離散型比較)

sql語(yǔ)句有幾種寫(xiě)法、效率、以及離散型 比較

1:SELECT * FROM tablename ORDER BY RAND() LIMIT 想要獲取的數(shù)據(jù)條數(shù);

2:SELECT *FROM `table` WHERE id >= (SELECT FLOOR( MAX(id) * RAND()) FROM `table` ) ORDER BY id LIMIT 想要獲取的數(shù)據(jù)條數(shù);

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 想要獲取的數(shù)據(jù)條數(shù);

4:SELECT * FROM `table`WHERE id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM `table`))) ORDER BY id LIMIT 想要獲取的數(shù)據(jù)條數(shù);

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 想要獲取的數(shù)據(jù)條數(shù);

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 想要獲取的數(shù)據(jù)條數(shù);

1的查詢時(shí)間>>2的查詢時(shí)間>>5的查詢時(shí)間>6的查詢時(shí)間>4的查詢時(shí)間>3的查詢時(shí)間,也就是3的效率最高。

以上6種只是單純的從效率上做了比較;

上面的6種隨機(jī)數(shù)抽取可分為2類(lèi):

第一個(gè)的離散型比較高,但是效率低;其他5個(gè)都效率比較高,但是存在離散性不高的問(wèn)題;

怎么解決效率和離散型都滿足條件啦?

我們有一個(gè)思路就是: 寫(xiě)一個(gè)存儲(chǔ)過(guò)程;

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

每次取出一條,然后循環(huán)寫(xiě)入一張臨時(shí)表中;最后返回 select 臨時(shí)表就OK;

這樣既滿足了效率又解決了離散型的問(wèn)題;可以兼并二者的優(yōu)點(diǎn);

下面是具體存儲(chǔ)過(guò)程的偽代碼

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

-- 新建一張臨時(shí)表 ,存放隨機(jī)取出的數(shù)據(jù)

create temporary table if not exists xdr_authen_tmp ( 
 `ID` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '序號(hào)',
 `LENGTH` int(5) DEFAULT NULL COMMENT '字節(jié)數(shù)',
 `INTERFACE` int(3) NOT NULL COMMENT '接口',
 `XDR_ID` varchar(32) NOT NULL COMMENT 'XDR ID',
 `MSISDN` varchar(32) DEFAULT NULL COMMENT '用戶號(hào)碼',
 `PROCEDURE_START_TIME` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '開(kāi)始時(shí)間',
 `PROCEDURE_END_TIME` datetime DEFAULT NULL COMMENT '結(jié)束時(shí)間',
 `SOURCE_NE_IP` varchar(39) DEFAULT NULL COMMENT '源網(wǎng)元IP',
 `SOURCE_NE_PORT` int(5) DEFAULT NULL COMMENT '源網(wǎng)元端口',
 `DESTINATION_NE_IP` varchar(39) DEFAULT NULL COMMENT '目的網(wǎng)元IP',
 `DESTINATION_NE_PORT` int(5) DEFAULT NULL COMMENT '目的網(wǎng)元端口',
 `INSERT_DATE` datetime DEFAULT NULL COMMENT '插入時(shí)間',
 `EXTEND1` varchar(50) DEFAULT NULL COMMENT '擴(kuò)展1',
 `EXTEND2` varchar(50) DEFAULT NULL COMMENT '擴(kuò)展2',
 `EXTEND3` varchar(50) DEFAULT NULL COMMENT '擴(kuò)展3',
 `EXTEND4` varchar(50) DEFAULT NULL COMMENT '擴(kuò)展4',
 `EXTEND5` varchar(50) DEFAULT NULL COMMENT '擴(kuò)展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是需要隨機(jī)獲取的數(shù)據(jù)數(shù),比如隨機(jī)獲取10條,那這里就是10,通過(guò)while循環(huán)來(lái)逐個(gè)獲取單個(gè)隨機(jī)記錄;

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;

-- 這里獲取的記錄有可能會(huì)重復(fù),如果是重復(fù)數(shù)據(jù),我們則不往臨時(shí)表中插入此條數(shù)據(jù),再進(jìn)行下一次隨機(jī)數(shù)據(jù)的獲取。依次類(lèi)推,直到隨機(jī)數(shù)據(jù)取夠?yàn)橹梗?/p>

 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; 

-- 最后我們將所有的隨機(jī)數(shù)查詢出來(lái),以結(jié)果集的形式返回給后臺(tái)

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分組后隨機(jī)獲取每組一條數(shù)據(jù)的操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 基于mysql實(shí)現(xiàn)group by取各分組最新一條數(shù)據(jù)
  • MySQL 子查詢和分組查詢
  • MySQL 分組查詢和聚合函數(shù)
  • MySql Group By對(duì)多個(gè)字段進(jìn)行分組的實(shí)現(xiàn)方法
  • 詳解MySQL分組排序求Top N
  • mysql獲取分組后每組的最大值實(shí)例詳解
  • Mysql利用group by分組排序
  • 詳解MySQL 數(shù)據(jù)分組

標(biāo)簽:阿里 沈陽(yáng) 天津 公主嶺 呼和浩特 牡丹江 合肥 惠州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《MySql分組后隨機(jī)獲取每組一條數(shù)據(jù)的操作》,本文關(guān)鍵詞  MySql,分組,后,隨機(jī),獲取,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《MySql分組后隨機(jī)獲取每組一條數(shù)據(jù)的操作》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于MySql分組后隨機(jī)獲取每組一條數(shù)據(jù)的操作的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章