怎么用一条sql查询不包含前5条记录的记录

如何用一条sql查询不包含前5条记录的记录?

用最新时间排序,然后最新的前5条记录不要查询,查询第6条之后的记录用一条sql怎么实现?



可考慮用存儲過程來實現:

DELIMITER $$

DROP PROCEDURE IF EXISTS `db`.`sp` $$

CREATE PROCEDURE `sp`()

begin

prepare stmt from ‘Select * from table limit 6,? ‘ ;

select count(*) into @inta from table;

execute stmt using @inta;

end $$

DELIMITER ;




首先判断纪录数是否大于5,如果小于等于,报纪录数不足.否则取倒序的前纪录数-5即可.这样可以吗?




你要不用存储过程只能这样写了。

select count(*) from 你的表 into @cnt;

select * from 你的表 where 1 = 1 order by 时间字段 desc limit 6,@cnt;



select * from table where ID not in ( select * from (select ID from table order by time limit 5) as t) order by time

id是主键。




select * from 你的表 where 1 order by 时间字段 desc limit 6,9999999

怎么用一条sql查询不包含前5条记录的记录

相关文章:

你感兴趣的文章:

标签云: