匹配行之前和之后的SQL N结果 [英] SQL N results before and after the matching row

查看:62
本文介绍了匹配行之前和之后的SQL N结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!



我在使用SQL语句时遇到问题。我在数据库中找到重复的东西,我想在结果之前和之后单独搜索每个条目+5行。我不知道怎么做这样的陈述,有人可以帮忙吗?



ps:我的行上没有ID。

解决方案

好吧,假设你有日期和sqlite,如前所述,这是一种可能的方法:

 选择 * 来自 
选择 * from d
其中​​ d< = ' 2013.01.07'
order by d desc
limit 6
union
选择 * 来自
选择 * 来自 d
其中 d> = ' 2013.01.07'
订单 d
limit 6



它将选择11个日期:

 2013.01.02 
2013.01.03
2013.01.04
2013.01.05
2013.01.06
2013.01.07
2013.01.08
2013.01.09
2013.01.10
2013.01.11
2013.01.12



它不是最干净的,但它有效。


Hello!

I'm having trouble with an SQL statement. I find duplicate entrys in the database and i want to search for each entry individualy + 5 rows before and after the result. I dont know how to make such a statement, could anybody help?

p.s.: I dont have ID's on the rows.

解决方案

Well, supposing you have dates and sqlite, as you mentioned before here is a possible approach:

select * from
(select * from d
where d <= '2013.01.07'
order by d desc
limit 6)
union
select * from
(select * from d
where d >= '2013.01.07'
order by d
limit 6)


It will select the eleven dates:

2013.01.02
2013.01.03
2013.01.04
2013.01.05
2013.01.06
2013.01.07
2013.01.08
2013.01.09
2013.01.10
2013.01.11
2013.01.12


It is not the cleanest, but it works.


这篇关于匹配行之前和之后的SQL N结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆