2016-07-22 36 views
0
與限隨機行

我的表像MySQL的獲取上值

表:

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

我想從表

我想導致像

id 0,1,2 
COL2的隨機值

意味着如果值爲0,那麼應該只有值爲0的2行只有不超過

回答

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() 
); 
+0

我想隨機值 –

+0

因爲COL2值並不侷限於它可能是與不重複的大於值0〜10個 –

+0

,我想10個隨機行3次 –