如何在mysql中随机选择2%的行?

bmvo0sr5  于 2021-07-27  发布在  Java
关注(0)|答案(3)|浏览(312)

如何从mysql中的表中随机获取2%的样本?
我尝试了以下查询

SELECT orderID
    FROM orders
    ORDER BY rand() 
    LIMIT 1 OFFSET (SELECT convert(0.02 * count(*), unsigned) FROM orders)
;

SELECT orderID
    FROM orders
    ORDER BY rand() 
    LIMIT (SELECT convert(0.02 * count(*), unsigned) FROM orders)
;

它们都给出了错误。有人能解释一下错误和替代方法吗。
数据库:北风
mysql版本:8.0

dzhpxtsq

dzhpxtsq1#

我会用 ROW_NUMBER 在这里:

WITH cte AS (
    SELECT *, ROW_NUMBER() OVER (ORDER BY RAND()) rn, COUNT(*) OVER () cnt
    FROM orders
)

SELECT *
FROM cte
WHERE rn < 0.02 * cnt;  -- select first 2% of a random sample
j8yoct9x

j8yoct9x2#

如果大约2%就足够了,那么您可以使用:

SELECT orderID
FROM orders
WHERE rand() <= 0.02;

因为不需要排序,这可能是最快的方法。
请注意,结果不完全是2%,但在一个大表上,它将是足够接近。

bkhjykvo

bkhjykvo3#

您可以使用mysql存储过程在mysql中创建函数。https://www.mysqltutorial.org/getting-started-with-mysql-stored-procedures.aspx/

CREATE PROCEDURE GetRand2PercentSample()
BEGIN
    DECLARE nLimit INT DEFAULT 0;

    SELECT COUNT(*) * 0.2
    INTO nLimit
    FROM orders;

    SELECT * FROM orders ORDER BY RAND() LIMIT nLimit;
END

// to call the function
call GetRand2PercentSample()

相关问题