可考慮用存儲過程來實現: 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 ;
你要不用存储过程只能这样写了。 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 timeid是主键。
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 ;
select count(*) from 你的表 into @cnt;
select * from 你的表 where 1 = 1 order by 时间字段 desc limit 6,@cnt;