sql查询问题:想查询一个表中符合某个条件值的记录的后一行记录的明细
sql问题:想查询一个表中符合某个条件值的记录明细:select * from T_D where C1+C2+C3=7
现在
想查询一个表中符合某个条件值的记录的后一行记录的明细,这该如何写sql语句啊!
比如符合条件的是第12行记录,我想要的是第13行的记录 --------------------编程问答--------------------
select top 1 * from tb where id>(select id from tb where id = 365) order by id asc--------------------编程问答-------------------- select top 1 from table1 where id not in (select top 12 from table1) 第十三条 --------------------编程问答-------------------- 在数据库中新增一个字段是ID,自增的。那样可以通过ID来得到你的数据!
select * from T_D where id = (select top1 id from T_D where C1+C2+C3=7)+1--------------------编程问答-------------------- to:aspwebchh
select id from tb where id = 365
条件所得记录不止一行记录
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
--------------------编程问答--------------------
条件里用你表的主键字段 那样就不重复了 --------------------编程问答-------------------- 子查询返回多个值1。这是不允许在子查询如下=,!=,<,<=,>,> =或子查询时,作为一个表达式中使用。 --------------------编程问答-------------------- 不好意思 刚才理解错误
那你可以加个 max函数 取最大的 就可以用 > 了 --------------------编程问答-------------------- select top 1 * from tb where id>(select top 1 id from tb where id = 1) order by id asc --------------------编程问答-------------------- select top 1 * from tb where id>(select id from tb where id = 1 order by id asc) order by id asc --------------------编程问答-------------------- select top 1 * from tb where id>(select top 1 id from tb where id = 1 order by id asc) order by id asc --------------------编程问答-------------------- 是这个意思??
--------------------编程问答-------------------- select * into #temp from T_D where C1+C2+C3=7
DECLARE @ta TABLE(Name varchar(10),title varchar(20))
insert into @ta
select 'a','123' union all
select 'a2','345' union all
select 'a11','231' union all
select 'a12','12311' union all
select 'a21','12323'
select IDEntity(int,1,1) ID,* INTO #temp from @ta where [name] like '%2%'
--满足条件数据
select * from #temp
--满足条件的第二条
select * from #temp where ID=2
drop Table #temp
/*
ID Name title
----------- ---------- --------------------
1 a2 345
2 a12 12311
3 a21 12323
(3 行受影响)
ID Name title
----------- ---------- --------------------
2 a12 12311
(1 行受影响)
*/
select top 1 * from #temp where id not in(select top 12 id from #temp) --------------------编程问答-------------------- sql 2005以上的可以用:
select * from (select row_number() over(order by id) as rownumber,* from T_D where C1+C2+C3=7) A where rownumber=13
其中id是必须用来排序的。 --------------------编程问答--------------------
顶 --------------------编程问答-------------------- select top 1 * from tb where id>(select id from tb where id = 365) order by id asc --------------------编程问答-------------------- select top 1 from newtable where id < newId
补充:.NET技术 , C#