有三个相关库表:
t_taskall: 存放全部的任务。(任务签出也不删除)
ID | SETS | DUID
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 2 3
. . .t_tasktemp: 任务临时表,存放用户请求签出的任务。它会是t_taskall的一个子集。(用户在签出时添加到该表,处理完后将任务从表中删除,并添加到t_taskdone)
ID | SETS | DUID
2 1 2
3 1 3t_taskdone: 存放用户处理完成的任务。只存放用户处理完成的任务,它会是t_taskall的一个子集。
ID | SETS | DUID
1 1 1
4 2 1
现在想检索出只在t_taskall中有, t_tasktemp 和 t_taskdone 都没有的,并且SETS最小的那些记录。如下:
ID | SETS | DUID
5 2 2
6 2 3
t_taskall: 存放全部的任务。(任务签出也不删除)
ID | SETS | DUID
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 2 3
. . .t_tasktemp: 任务临时表,存放用户请求签出的任务。它会是t_taskall的一个子集。(用户在签出时添加到该表,处理完后将任务从表中删除,并添加到t_taskdone)
ID | SETS | DUID
2 1 2
3 1 3t_taskdone: 存放用户处理完成的任务。只存放用户处理完成的任务,它会是t_taskall的一个子集。
ID | SETS | DUID
1 1 1
4 2 1
现在想检索出只在t_taskall中有, t_tasktemp 和 t_taskdone 都没有的,并且SETS最小的那些记录。如下:
ID | SETS | DUID
5 2 2
6 2 3
from t_taskall A
where id not in (select id from t_tasktemp) and id not in (select id from t_taskdone)
and not exists (seelct 1 from t_taskall where A.set>set)
left join t_taskdone c on a.sets=c.sets and a.duid=c.duid
where b.duid is null and c.duid is null
如果是这样的话。检索出来的数据包括了没有在`t_taskdone`,``t_tasktemp`二个表里的记录。但还是不是只有最小的SETS记录。
where ID not in (select ID from t_tasktemp)
and ID not in (select ID from t_taskdone)
ID | SETS | DUID
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 2 3
7 3 1
7 3 2
. . .使用前面几位的检索方法检出来的数据不符合我的要求。
ID | SETS | DUID
5 2 2
6 2 3
7 3 1
7 3 2
. . .我想得到的是ID | SETS | DUID
5 2 2
6 2 3
left join t_taskdone c on a.sets=c.sets and a.duid=c.duid
where b.duid is null and c.duid is null存为VIEW1
select * from view1 a where not exists(select 1 from view1 where a.sets>sets)
select min(a.sets) as mi from t_taskall a left join t_tasktemp b on a.sets=b.sets and a.duid=b.duid
left join t_taskdone c on a.sets=c.sets and a.duid=c.duid
where b.duid is null and c.duid is null) c on a1.sets=c.mi
where ID not in (select ID from t_tasktemp)
and ID not in (select ID from t_taskdone)
and not exists (select 1 from t_taskall where SETS< t.SETS)