我有个student表,id为1~1000,我想随机找出其中的一条记录,用下面的sql
select * from student where id=(select floor(1+rand()*1000));
结果有时候显示为0条,有时候居然能返回两条数据,咋回事阿?不是限定了id等于一个数字吗?怎么还能返回两条记录了?
我又试过select * from student where id>=(select floor(1+rand()*1000)) order by id limit 1;基本上只返回id为100以内的一条记录,上百的压根就看不到,请问怎么写才能比较均匀的返回1~1000之间的记录呢?
select * from student where id=(select floor(1+rand()*1000));
结果有时候显示为0条,有时候居然能返回两条数据,咋回事阿?不是限定了id等于一个数字吗?怎么还能返回两条记录了?
我又试过select * from student where id>=(select floor(1+rand()*1000)) order by id limit 1;基本上只返回id为100以内的一条记录,上百的压根就看不到,请问怎么写才能比较均匀的返回1~1000之间的记录呢?
WHERE id>=RAND()*1000
ORDER BY id
LIMIT 1;
use school;
CREATE TABLE student (id INT(10) NOT NULL PRIMARY KEY,name CHAR(20),birthday DATE);#创建存储过程,插入1000条数据
delimiter $$
create procedure pro1()
begin
declare i int default 0;
set i=0;
while i<1000 do
insert into student (id,name,birthday) values (i,'abc','2010-7-8');
set i=i+1;
end while;
end;
$$
delimiter ;#调用存储过程
call pro1();
SELECT CEIL(RAND()*1000) AS ID) b ON a.id=b.id ;
update student set name='Jame' where id>=(select floor(1+rand()*1000)) order by id limit 1; 是肯定不行的。