㈠ 请教一段能同时查出上一条记录及下一条记录的sql
如果有主键自增或者按时间排序的数据库记录的话
直接根据当前的记录的主键或时间,大概可以这样写
(select * from table where id > 当前id order by id asc limit 1 ) union
(select * from table where id < 当前id order by id desc limit 1)
这样差不多能满足
㈡ 关于查询sql中数据上一条记录和下一条记录的sql语句......
sql中是设置的自增列啊;不需输入当前的id,这里默认的是1;要是是1的话,就只能查到第2个,但3个就不晓得了回答: 当你查到第2个的时候,就获取第二条的ID,点下一条的时候,就用第二条的id去查,后边的类推~追问: 主要就是现在的id的获取问题回答: 第一条就以0开始就行,后边就获取查出来的数据的id就行了, 你数据都查出来了,还获取不到id?追问: id = Request.QueryString["id"]; 我是用这个获取的;不知对否回答: 你能获取到值就可以了,调试一下就行了~追问: 关键就是无法获取到值..问题就出现在这里了,卡起了回答:
㈢ [sql]上下记录间相同的数,如何统计!求助高手!~
这芦悔么有难度的题一分都没有哦
表里面一定要加个排序列!不然消哗码没有规则
/*
-----建表
create table aa(
id int not null identity(1,1) primary key,
n1 int not null,
n2 int not null,
n3 int not null,
n4 int not null
)
---数据
insert into aa(n1,n2,n3,n4) values(5,17,20,25)
insert into aa(n1,n2,n3,n4) values(1,15,25,30)
insert into aa(n1,n2,n3,n4) values(2,3,11,26)
insert into aa(n1,n2,n3,n4) values(3,11,26,50)
insert into aa(n1,n2,n3,n4) values(10,12,26,29)
*/
---查拿哪询
select a.* ,
isnull((
select convert(varchar(16),disb.n)+',' from
(select n1 as n from aa where id=a.id or id=a.id-1 union all
select n2 as n from aa where id=a.id or id=a.id-1 union all
select n3 as n from aa where id=a.id or id=a.id-1 union all
select n4 as n from aa where id=a.id or id=a.id-1 )disb group by n having count(*)>1 order by n for xml path('')
),'')n,
(select count(n) from (
select n from
(select n1 as n from aa where id=a.id or id=a.id-1 union all
select n2 as n from aa where id=a.id or id=a.id-1 union all
select n3 as n from aa where id=a.id or id=a.id-1 union all
select n4 as n from aa where id=a.id or id=a.id-1 )disb group by n having count(*)>1
)n)gs
from aa a
--结果
id n1 n2 n3 n4 n gs
1 5 17 20 25 0
2 1 15 25 30 25, 1
3 2 3 11 26 0
4 3 11 26 50 3,11,26, 3
5 10 12 26 29 26, 1
(5 行受影响)
网络lj 结果对不齐
㈣ 关于查询sql中数据上一条记录和下一条记录的sql语句......
可用row_number来解决。
如student表
id name create_date
1 张三 2015-07-01
2 李四 2015-06-01
3 王五 2015-08-01
4 赵六 2015-04-01
如,要查找张三的create_date前和后各一条数据。
withtas
(selectstudent.*,row_number()over(orderbycreate_date)rnfromstudent)
select*fromstudentwherern=(selectt.rn+1fromtwheret.name='张三')
unionall
select*fromstudentwherern=(selectt.rn-1fromtwheret.name='张三')
结果应是:
id name create_date
2 李四 2015-06-01
3 王五 2015-08-01
㈤ SQL如何获取上一条..下一条..首尾记录...
获得上一条的id :select max(id)as id from [表] where id<"[你的要查的id]" order by [.....]
获得下一条的id :select min(id)as id from [表] where id>"[你的要查的id]" order by [.....]
很笨的办法但是很直观·
不知道你是什么数据库··根据不同的数据库有很多不同的写法··
比如 mysql 中的 limit 或者 mssql 中的 top
写法多了去啦··呵呵··上面举个例子罢了··希望对你有帮助
㈥ Access数据库的上下条记录能相互计算吗比如用SQL查询:当前记录与下一条记录的比值=已知值。请帮助解答
可以的,放在where条件中就可以了!缓歼孝
select * from tablename where tablename.col1/tablename2 = value
如果类型不同需要进行一次转换的!
如col1是int,而col2也是int,你要的值是一个其他类型时,就用:
select* from tablename where convert(类型,col1/col2[,长度]) = value
也就是说还要会用convert进行类型之间的强制转换!
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
看错了,你说的上下条记录是吧?
答案是没有任何办法的。因为在关系型数据库中是不存在下下条关系的,每条记录都相对一个实体,而记录与记录之间没有任何关系的。只是习惯上我们可以对其进行排序,但无法定义进具体的上下条的,(存储顺序还是某一条件的排序?)关系数据库在设计时就不考虑某相互之间的关系,也就没有所谓的上下条的,你可以看到换一种排序方法,上下的条的关系就改变了,扰稿或者说不同的顺序表示的数据库是一样的。所以没有具体实体间联系。
不过可以程改源序进行实现这种不符合数据库数据的办法的!
㈦ 谁给我写个sql语句:关于上一条记录和下一条记录的对比
select b.*,
(case when (b.s_time-a.s_time)*1440>2 then 2
else 0 end ) --下一条记录与上一条记录的时间差--分钟
from
(select a.*,
row_number() over(partition by servier_id order by service_id,s_time desc) roworder
from t1
) a,
(select a.*,
row_number() over(partition by servier_id order by service_id,s_time desc) roworder
from t1
) b
where a.servicer_id=b.service_id and b.roworder=a.roworder+1;
其中
t1表名
servier_id 是一个关键,比如一个员工的ID
s_time 记录时间
做两个子查询,对每一个员工的记录都按时间倒序排列
where条件把同一员工的前后两条记录关联起来。
㈧ sql语句查询,某一记录上下相邻的两条记录。怎么写
select * from news a
where news_id > 12345 and not exists(select 1 from news where news_id > 12345 and
news_id < a.id )
or
news_id < 12345 and not exists(select 1 from news where news_id < 12345 and
news_id > a.id )
如果不需要用一条语句写出来,那分开写的话,效率会好一些,这样写必须用一个or关键字
分开写:
select max(news_id) from news where news_id < 12345
select min(news_id) from news where news_id > 12345
㈨ SQL 求一列中上下记录差的绝对值
select a.id,a.n, ( select b.n-a.n from aa b where b.id=a.id+1) cha
from aa a
where ...
差不多这样了,我没有测试。 提醒到晌如这样,应该你可以debug出来了。
对了,要绝对值的话亮滑,找宴键启abs用下一下即可。
㈩ 如何获取SQL查询当前数据上一条和下一条的记录
方法一:x0dx0a查询上一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误):x0dx0a1x0dx0aselect * 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];x0dx0a查询下一条记录的SQL语句(如果有其他的查询条件记得加上other_conditions以免出现不必要的错误):x0dx0a1x0dx0aselect * from table_a where id = (select id from table_a where id > {$id} [and other_conditions] order by id asc limit 1) [and other_conditions];