如果ID是主键或者有索引,可以直接查找: 方法一: 查询上一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误): select * from table_a where id = (select id from table_a where id < {$id} [and other_conditions] order by id desc limit 1) [and other_conditions]; 查询下一条记录的SQL语句(如果有其他的
需求: 如何将多条update语句合并为一条update语句:如,update table1 set col='2012' where id='2014001' update table1 set col='1009' where id='2014003'如何合并为一条? 在网上找了好久,总结了一个相对简单的语句(有些语句是函数语句,有点晕),如下: update table1 set col=(case id when '2014001' then '2012' when '2
-- 演示将多条记录数据组合成一条sql插入语句(for mysql) function getTpl0(tname) -- 获取表各个字段 local t = { tpl_pack = {"packId","itemId","`group`","num","rate","rateType"}, } for k, v in pairs(t) do if tname == k then r
偶然看到某些网站在新闻详情中 ,往往是需要根据当前信息id获取到上一条信息和下一条信息的,而通常我们的做法是先获取当前信息,再获取上一条信息,再获取下一条信息,就需要发送三次查询才能够得到这些信息,一些编程网站或者论坛给出的方案是用SQL union写的. 比如当前id为5 想获取当前信息 上一条信息 下一条信息就是 ( ) union ( ) union ( ) 结果如图 而如果这样写觉得不太好的话也可以写成这样 select * from news where id in ( select
sql如何查询表的第一条记录和最后一条记录 方法一:使用top select TOP 1 * from apple;TOP 1 表示表apple中的第一条数据 select TOP 1 * from apple order by id desc;TOP也是第一条数据,但是order by(以什么排序),id desc 以id的降序排列,所以要是id要是增长的话,最后一条数据就被排到第一条了 (备注:top是Access的语法,MySQL不支持) 方法二:使用LIMIT 第一条记录 mysql>
上一条:select * from 表 where 数据id<@当前显示数据id order by 数据_id asc) limit 1下一条:select * from 表 where 数据id>@当前显示数据id order by 数据_id desc) limit 1 mysql 里面不支持 select top
上一条记录的SQL语句: * from news where newsid<id order by newsid DESC 下一条记录的SQL语句: * from news where newsid>id order by newsid ASC 开发中遇到需要在当前页面显示当前文章的上一篇文章和下一篇文章,百度了一下,搜索到以上SQL语句:
<?php class czy { public $host="localhost"; //地址 public $uid="root"; //用户名 public $pwd="*******";//用户密码 public $dbname="*******";//用户数据库名 /** *给一个sql语句,返回执行的结果 *@param string @sql用户指定的sql语句 *@param int $type 用户给的
一.条件字段为数值的情况 select * from tb where id=@id; --当前记录 select top 1 * from tb where id>@id order by id; --下一条记录 select top 1 * from tb where id<@id order by id desc --上一条记录 二.以排序的思路出发的一种方案 ;WITH TUsers AS ( SELECT *, ROW_NUMBER() OVER (