您好,登錄后才能下訂單哦!
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 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分組后如何隨機獲取每組一條數據問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。