查询数据表中间N条记录的sql语句
大家别笑,我一计算机类专业应届毕业生,但是学得不怎么好,面试经常被BS
笔试曾经有一题:
写出一条Sql语句:取出表A中第31到第40记录(SQLServer,以自动增长的ID作为主键,注意:ID可能不是连续的。
现在知道在SQL Server 或者 access上可以这样写:
select top 40 ID
from admin where ID not in(select top 30 ID from admin) ;
但是最近在弄mysql,想到了这题,但是试了一下在mysql下这样写:
select ID from admin limit 40 where id not in(select ID from admin limit 30)
结果证明是在子查询中是不能使用limit的
然后在网查了一下,可以这样在子查询中使用limit:
select ID from admin where ID in (select t.ID from (select ID from admin limit 40)as t)
然后就试了一下这样写用于返回中间6-10条记录:
select * from admin where id not in(select t.id from(select * from admin limit 4) as t) limit 6;
不知道各位牛人有什么更好的办法!