public function prevnext($table,$id,$where=[]){ $ids=db($table)->field('id,title')->order('sort asc')->where($where)->column('id'); $key=array_search($id,$ids); // 上一条 if(isset($ids[$key-1])){ $prevnext['prev']=db($table)->field('id,title,c…
select id from(select *, (@i:=@i+1) as rownum from pre_bet_zhibo,(select @i:=0) as itwhere link_conent like '%足%'order by title) as t1 where rownum =(select rownum from(select *, (@i:=@i+1) as rownum from pre_bet_zhibo,(select @i:=0) as itwhere link_…
偶然看到某些网站在新闻详情中 ,往往是需要根据当前信息id获取到上一条信息和下一条信息的,而通常我们的做法是先获取当前信息,再获取上一条信息,再获取下一条信息,就需要发送三次查询才能够得到这些信息,一些编程网站或者论坛给出的方案是用SQL union写的. 比如当前id为5 想获取当前信息 上一条信息 下一条信息就是 ( ) union ( ) union ( ) 结果如图 而如果这样写觉得不太好的话也可以写成这样 select * from news where id in ( select…
获取当前文件上一条与下一条记录的原理是上一条的sql语句,从news表里按从大到小的顺序选择一条比当前ID小的新闻,下一条的sql语句,从news表里按从小到大的顺序选择一条比当前ID大的新闻. 如果ID是主键或者有索引,可以直接查找: 方法1: 1.select * from table_a where id = (select id from table_a where id < {$id} order by id desc limit 1); 2.select * from table_a…
上一条记录的SQL语句: * from news where newsid<id order by newsid DESC 下一条记录的SQL语句: * from news where newsid>id order by newsid ASC 开发中遇到需要在当前页面显示当前文章的上一篇文章和下一篇文章,百度了一下,搜索到以上SQL语句:…