表a中有字段 status,user,operator_time ,id。其中id为主键,status状态有1,2,3。我想查询出用户为admin,状态为1,操作时间最小的那条记录。其中admin在状态为1可能对应多条记录,他们的操作时间不同。

解决方案 »

  1.   

    select * from a b where not exists(select 1 from a c where b.oprator_time<c.operator_time) and user='admin' and status=1;
      

  2.   

    select * from a where operator_time=(
    select min(operator_time) from a where user='admin' and status='1')
    and user='admin' and status='1';
      

  3.   

    --1楼的思路不错,学习了
    --但需要改一下
    select * from a b where not exists(select 1 from a c where b.oprator_time >c.operator_time where c.user=b.user and c.status=b.status)
     and user='admin' and status=1;
      

  4.   

    能解释下 not exists(select 1 from a c where b.oprator_time >c.operator_time 这里为什么这么写吗?
      

  5.   

    嗯,我考虑不周,sorry,忘了加条件了
      

  6.   

    select * from  a where   status='1' AND user='admin' and operator_time =(select min(operator_time ) as operator_time from  a where   status='1' AND user='admin' ) 
     
     
      

  7.   

    select * from (select * from  a where  status='1' AND user='admin' order by operator_time) where rownum=1; 
      

  8.   

    select * from
    a
    where (id,operator_time) in
    (
    select id,min(operator_time)
    from a
    where user='admin' and status='1'
    group by (id)
    );
      

  9.   

    这些东西你可以看下面这个文章:http://blog.csdn.net/huangyunzeng2008/archive/2009/10/08/4643598.aspx