從資料表隨機拉取資料,單條,多條

2022-05-03 07:42:09 字數 447 閱讀 1300

1、拉取單條資料

切忌(甚至可以說是錯誤)用法:

select * from `table_name` order by rand() limit 1    (切忌使用)

注:在 order by從句裡面不能使用rand()函式,因為這樣會導致資料列被多次掃瞄,導致效率相當相當的低

"select * from table_name as t1

join (select round(rand() * ((select max(id) from table_name)-(select min(id) from table_name))+(select min(id) from table_name)) as id)

as t2 where t1.id >= t2.id order by t1.id limit 1";

從mysql資料表中隨機取出一條記錄

核心查詢資料表 select from 表名 order byrand limit 1 此處的1就是取出資料的條數 但這樣取資料網上有人說效率非常差的,那麼要如何改進呢 搜尋google,網上基本上都是查詢max id rand 來隨機獲取資料。select from table as t1 joi...

MySql從表結果集中隨機取一條資料

該功能的實現基於 mysql 查詢結果中自動加序號列 select from select rowno rowno 1 as rowno,pwd from pur admininfo,select rowno 0 b tmp where rowno select round rand 1000000...

MySql從表結果集中隨機取一條資料

該功能的實現基於 mysql 查詢結果中自動加序號列 select from select rowno rowno 1 as rowno,pwd from pur admininfo,select rowno 0 b tmp where rowno select round rand 1000000...