简体   繁体   English

随机 select 一半的记录

[英]Randomly select half of records

I'm trying to generate a random sample of half of a table (or some other percentage).我正在尝试生成一半表(或其他百分比)的随机样本。 The table is small enough that I can use the ORDER BY RAND() LIMIT x approach.该表足够小,我可以使用ORDER BY RAND() LIMIT x方法。 I'd like the code to sample 50% of recipients as the table changes size over time.我希望代码对 50% 的收件人进行抽样,因为表格会随着时间的推移而改变大小。 Below was my first attempt but you can't put a subquery in a LIMIT clause.以下是我的第一次尝试,但您不能将子查询放在 LIMIT 子句中。 Any ideas?有任何想法吗?

SELECT
    recipient_id
FROM
    recipient
ORDER BY RAND()
LIMIT (
    /* Find out how many recipients are on half the list */
    SELECT
        COUNT(*) / 2
    FROM
        recipient
);

If you are running MysQL 8.0, you can use window functions:如果您正在运行 MysQL 8.0,则可以使用 window 函数:

select *
from (select t.*, ntile(2) over(order by random()) nt from mytable t) t
where nt = 1

In earlier versions, one approach uses user variables:在早期版本中,一种方法使用用户变量:

select t.*
from (
    select t.*, @rn := @rn + 1 rn
    from (select * from mytable order by random()) t
    cross join (select @rn := 0) x
) t
inner join (select count(*) cnt from mytable) c on t.rn <= c.cnt / 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM