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

解决方案 »

  1.   

    SELECT `t_taskall`.`ID`,`t_taskall`.`SETS`,`t_taskall`.`DUID` FROM `t_taskall`,`t_taskdone`,``t_tasktemp` WHERE `t_taskall`.`ID` NOT IN (SELECT `t_taskdone`.`ID` FROM `t_taskdone`) AND NOT IN (SELECT `t_tasktemp`.`ID ` FROM `t_tasktemp`) ORDER BY `t_taskall`.`SETS`;
      

  2.   

    select *
    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)
      

  3.   

    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
      

  4.   


    如果是这样的话。检索出来的数据包括了没有在`t_taskdone`,``t_tasktemp`二个表里的记录。但还是不是只有最小的SETS记录。
      

  5.   

    select * from t_taskall
    where ID not in (select ID from t_tasktemp)
    and  ID not in (select ID from t_taskdone)
      

  6.   

    可能是我的表述并不清楚。楼上的几位只检索出了没有在`t_taskdone`,``t_tasktemp表里的记录。如果表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
    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
      

  7.   

    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)
      

  8.   

    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.mi
      

  9.   

    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)
      

  10.   

    虽然各位给出的答案都不算正确。但各位的思路给了我很大帮助。最终采用了wwwwb的思路。先建立一个VIEW,把符合条件的记录都检出来,再用MIN子查询把最小SET的所有记录检出来。