问题

1
2
3
set @r = (select floor(rand() * (@c - 1)));
select * from proxy limit @r,1;
MySQL的limit参数不能使用变量

解决办法

SQL语句改为prepare字符串,其中变量为参数,传入参数后执行prepare

1
2
3
4
5
set @c = (select count(*)from proxy);
set @r = (select floor(rand() * (@c - 1)));
prepare ps from 'select * from proxy limit ?,1';
execute ps using @r;
deallocate prepare ps;