这个问题简化如下:
表table
-----------------
id    value    status
1     20       0
1     10       -1
2     15       -1
2     30       0
3     60       -1
3     55       0
...............
要求结果为
id    value    status
1     10       -1
2     15       -1
即对id分组,每组中取value最小且status=-1的记录

解决方案 »

  1.   

    select id,min(value),-1 as  status
    from 表table
    where status=-1
    group by id
      

  2.   

    感谢回复
    但结果只是把status=-1的列出来了:
    id  min( value )  status  
    1   10            -1 
    2   15            -1 
    3   60            -1 =>此行不能要,因为60不是本组中最小的
    本句SQL的问题应该是where status=-1不能有
      

  3.   

    假设表名为:tb;select a.* from tb as a where a.status = -1 and a.value = (select min(value) from tb where id = a.id)