先举个例子,现在有这么一条语句:
select a.* from table a where a.date1=(select min(date1) from table where type=a.type and id>a.id)
大家知道这语句是选择出当前记录的下一条记录(id是自增序号,日期也是增长的,假设没有相同的日期)
现在有一个问题,如果当前的记录恰好是这个type的最后一条记录,那么这条记录始终会被选择出来,而实际上按需求是不该被选择到的
这应该怎么做?
select a.* from table a where a.date1=(select min(date1) from table where type=a.type and id>a.id)
大家知道这语句是选择出当前记录的下一条记录(id是自增序号,日期也是增长的,假设没有相同的日期)
现在有一个问题,如果当前的记录恰好是这个type的最后一条记录,那么这条记录始终会被选择出来,而实际上按需求是不该被选择到的
这应该怎么做?
select * from tab a
where exists(select 1 from tab b where a.type=b.type and a.id>b.id)
这应该怎么做?你这个需求不对,当前的记录?
给个例子
id a字段 b字段 date type
1 58 1 07-5-5 2
2 52 6 07-5-6 2
3 50 1 07-5-7 2
4 42 8 07-5-4 2
5 37 5 07-5-9 2
很明显,第3,4条记录是错误的
第3条:50+1不等于第2条的52
第4条:日期错误了