有三个相关库表:
t_taskall: 存放全部的任务。(任务签出也不删除)
ID | SETS | DUID
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 2 3
7 3 1
8 3 2
9 3 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
7 3 1
8 3 2
9 3 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
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)好可以 用MIN
select a1.* from t_taskall a1 inner join (
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.miorselect a1.* from t_taskall a1 inner join (
select 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 order by a.sets limit 1) 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)
select * from t_taskall t where
not exists(select 1 from t_tasktemp a where a.Id=t.id and a.sets=t.sets and a.buid=t.buid)
and not exists(select 1 from t_taskdone b on b.id=t.id and b.sets=t.sets and b.buid=t.buid)