mysql 记录 上一行 下一行

2010-05-24 15:43  729人阅读  评论 (0)
Tags: mysql

mysql 前一行 后一行

CREATE TABLE `test`.`tab` (   
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,   
`val` INT NOT NULL   
) ENGINE = MYISAM ;   

insert into tab values(null, rand());   
insert into tab values(null, rand());   
insert into tab values(null, rand());   
insert into tab values(null, rand());   
insert into tab values(null, rand());   

delete from tab where id in (2,4);   

select max(if(t1.id>t2.id, t2.id, null)) as after, t1.id, min(if(t1.id<t2.id, t2.id, null)) as first from tab as t1, tab as t2 where t1.id = 3;   

select max(if(t1.id>t2.id, t2.id, null)) as after, t1.id, min(if(t1.id<t2.id, t2.id, null)) as first from tab as t1, tab as t2 group by t1.id;

性能不知道高不高,我也没有做详细测试!只是给大家一个灵感。