SQL: UNION ALL / rand() / using
1. 随机取3条记录,不连续:
select * from tbA JOIN (SELECT ceil(rand() * (select max(id) from tbA)) as ID) as r2 using (id) UNION ALL
select * from tbA JOIN (SELECT ceil(rand() * (select max(id) from tbA)) as ID) as r2 using (id) UNION ALL
select * from tbA JOIN (SELECT ceil(rand() * (select max(id) from tbA)) as ID) as r2 using (id) ;
这里的using(id)是什么意思?
2. 得到随机的3条连续记录:
select * from tbA as a join (select round(rand()* ((select max(id) from tbA where type=1)-(select min(id) from resume_message where type=1))+(select min(id) from resume_message where type=1)) as nid) as b where a.id>=b.nid and type=1 limit 3;