mysql获取值为

时间:2016-07-22 12:00:25

标签: mysql mysqli

我的桌子就像

表:

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

我想要表格中的col2的随机值

我想要像

这样的结果
id 0,1,2

表示如果value为0,则值0应该只有2行,但不超过

1 个答案:

答案 0 :(得分:0)

可能最简单的方法是使用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()
);