gpt4 book ai didi

mysql - 获取具有值限制的随机行

转载 作者:行者123 更新时间:2023-11-29 20:43:03 24 4
gpt4 key购买 nike

我的 table 就像

表:

id    col1 col2
0 a 0
1 a 0
2 a 1
3 a 0

我想要表中 col2 的随机值

我想要这样的结果

id 0,1,2

意味着如果一个值为 0,那么值 0 应该只有 2 行,最多不能超过该值

最佳答案

可能最简单的方法是使用union all:

(select t.*
from t
where col2 = 0
order by rand()
limit 2
) union all
(select t.*
from t
where col2 <> 0
order by rand()
);

关于mysql - 获取具有值限制的随机行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38525910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com