前言:
而今姐妹们对“mysql取第一个和最后一个”都比较关心,大家都需要学习一些“mysql取第一个和最后一个”的相关资讯。那么小编也在网上网罗了一些关于“mysql取第一个和最后一个””的相关文章,希望朋友们能喜欢,我们快快来学习一下吧!在开发中,很多公司用的ID为自增的,上一条下一条的获取方法不在此类,一般如
select * from tablename where id<$id order by id des limit 1; 下一条
select * from tablename where id>$id order by id asc limit 1; 上一条
公司的商品ID用的是uuid,用以前的方式取上一条下一条的方式已不行 如:id<$id id>$id
下一条
select * from
( select *, (@i:=@i+1) as rownum from shop_commodity,(select @i:=0) as it order by up_time DESC )
as t1 where rownum =((
select rownum from
(
select *, (@j:=@j+1) as rownum from shop_commodity,(select @j:=0) as it
order by up_time DESC
) as tb
where id = '004b76p5pxv063762910'
) + 1
);
上一条
select * from
( select *, (@i:=@i+1) as rownum from shop_commodity,(select @i:=0) as it order by up_time DESC )
as t1 where rownum =((
select rownum from
(
select *, (@j:=@j+1) as rownum from shop_commodity,(select @j:=0) as it
order by up_time DESC
) as tb
where id = '004b76p5pxv063762910'
) - 1
);
Oracle中有一个伪列rownum,可以在生成查询结果表的时候生成一组递增的序列号。MySQL中没有这个伪列,但是有时候要用,可以用如下方法模拟生成一列自增序号。
sql示例:select (@i:=@i+5) as rownum, surname, personal_name from student, (select @i:=100) as init;
用来增加一例,且 rownum 以100为基础,每一次加5.
标签: #mysql取第一个和最后一个