MySql從表結果集中隨機取一條數據
[sql] Select * from (Select (@rowNo :=@rowNo+1) AS rowno, pwd from pur_admininfo, (Select @rowNo := 0) b) tmp where rowno=(Select Round(Rand() * 10000000 % (Select Count(*) from pur_admininfo))) limit 1;
為了可以隨機得到表結果集中的任何一條記錄,這裡乘以10000000,然後再取模,但不知道為什麼更大的數字就不行了。 另一種實現方式:
[sql] SELECT * FROM pur_admininfo AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(rid) FROM pur_admininfo) - (SELECT MIN(rid) FROM pur_admininfo)) + (SELECT MIN(rid) FROM pur_admininfo)) AS rid) AS t2 WHERE t1.rid >= t2.rid ORDER BY t1.rid LIMIT 1;