表 table
字段 id(唯一标识)     name       class   date   state
      1               a            61     --     --
      2               b            61     --     --
      3               a            61     --     --
      4               a            61     --     --使用 select distinct name,class from table where class=61
的话只能显示name,class这2个字段
如果我想所有字段的sql语句要怎么写?
效果-----------------------------------------------------
字段 id(唯一标识)     name       class   date   state
      1               a            61     --     --
      2               b            61     --     --

解决方案 »

  1.   

    --是这个意思?
    select * from table where (name,class)
    in (select distinct name,class from table where class=61)
      

  2.   

    用分析函数
    with t1 as (select a.*,row_number()over(partition by name ,class order by id asc) rn  from table a)
    select * from t1 where rn=1
      

  3.   

    有没有只用sql语句就能搞定的???
      

  4.   

    我是要在java里面写sql的
    你那种写法貌似行不通
    能用一条sql语句搞定吗??
      

  5.   

    select * from (select a.*,row_number()over(partition by name ,class order by id asc) rn from table a)  where rn=1
      

  6.   


    select * from tb a where not exists(select 1 from tb b where a.name=b.name and a.id>b.id)select *
    from (select a.*,row_number() over(partition by a.name order by id) rn
    from tb a)
    where rn=1