有三个相关库表:
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

解决方案 »

  1.   

    select a.* 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存为VIEW1
    select * from view1 a where not exists(select 1 from view1 where a.sets>sets)好可以 用MIN
      

  2.   

    or
    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
      

  3.   

    select * from t_taskall t
    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)
      

  4.   


    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)
      

  5.   

    楼上各位的答案都不符合我的要求,但各位的思路给了我很大帮助。最终采用了WWWWB的方式。先建一个VIEW检出只存于All表的记录,再对于VIEW用MIN子查询,把最小SET对应的所有记录检出来。效果还未可知。