有一数据库,内容大致如下:
ID(PK)    NAME    TIME   STATUS
1          ab      ..     wait
2          ab      ..     execute
3          ab      ..     end
4          cd      ..     wait
5          cd      ..     execute
6          ef      ..     wait
7          ef      ..     execute
8          ef      ..     end
每一个NAME相同的记录 如 数据库中有三条NAME为ab的记录,有三种状态, wait, execute, end.
现在需要要查找出状态只有wait,没有end的记录,比如NAME为 cd 的记录只有两条,状态是wait和 execute,没有end状态,请问要找出这样的记录SELECT语句要怎么写,请指点,谢谢~`

解决方案 »

  1.   

    select * from tableName where STATUS='wait' and id not in(select id from tableName  where STATUS='end' )
      

  2.   

    select * from tableName where STATUS='wait' and Name not in(select distinct Name from tableName  where STATUS='end' )
      

  3.   

    要查找出状态只有wait,没有end的记录
    --------------------------------
    那是不是NAME为 cd的也不满足条件?
      

  4.   

    如果是想得到cd而不是上面我说的意思,那么可以这样写:select * from tb g 
    where STATUS='wait' 
    and not exists(select 1 from tb where NAME=g.NAME and STATUS='end' )