因为mysql没有top的用法,而且limit后也不支持子查询所以实现相对麻烦,
可以使用类似于Oracle中伪列的用法

select @rownum:=@rownum+1,student.* 
from (select @rownum:=0) row ,student;

可以看到表中有5条数数据
在这里插入图片描述

select round(count(*)/4) from student

在这里插入图片描述
补充一下这里的rownum只是个变量名,也可以是用其他的,不一定非得@rownum

select @rownum:=@rownum+1,student.* 
from (select @rownum:=0) row ,student
where @rownum<(select round(count(*)/4) from student);//查询列号小于25%的数据

在这里插入图片描述
mysql不支持top和rowid感觉挺伤的

Logo

更多推荐