我有一个表ID  content
1   a
2   a
3   a
4   b
5   b
6   c
7   c我要怎样才能得到ID  content
1   a
4   b
6   c  这样的结果(保留与a、b、c相应的ID最小的那个值)?若用select distinct则只能压缩content列,不能提取与content相应的ID列的最小值望帮忙,先此谢过

解决方案 »

  1.   

    既然是找最小值,那就要用min()函数select min(ID),content from table group by content
      

  2.   

    select content,min(id)
    from table1
    group by content
      

  3.   

    select min(id),content from Table group by content 
    或者
    select id,content from table a where not exists(select 1 from table b where a.content=b.content and a.id>b.id)
      

  4.   

    declare @t table(id int , content varchar(8))insert into @t
    select 1,   'a' union 
    select 2,   'a' union
    select 3,   'a' union
    select 4,   'b' union
    select 5,   'b' union
    select 6,   'c' union
    select 7,   'c'select content , min(id) as c
    from @t a
    group by content
    order by c
      

  5.   

    谢过楼上各位,另,distinct总是把大小写合并,请问如何令它区分大小写呢?即把下表content
    aa
    Aa
    aa
    b
    B
    c
    c通过一种办法变为content
    aa
    Aa
    b
    B
    c呢? 再次感谢楼上两位兄台
      

  6.   

    select * from tb tc where not exists(select 1 from tb where ID<tc.ID and content=tc.content)
      

  7.   

    谢过楼上这位,但我还是闹不明白,我干脆点,问题1:如何根据下面这个表ID  content
    1   aa
    2   Aa
    3   aa
    4   b
    5   B
    6   c
    7   c如何在区分大小写的情况下压缩content列,并取得相应的最小的ID值,生成下表ID  content
    1   aa
    2   Aa
    4   b
    5   B
    6   c问题2:根据字符进行的join连接好像也不区分大小写,怎么从根本上解决SQL根据字符进行查询时不区分大小字符的问题。再次劳驾各位大虾伸出援手,非常感谢
      

  8.   

    select min(id) ,min(content)
    from @t
    group by content  collate Chinese_PRC_CS_AS_WS 
      

  9.   

    select min(id) ,content  collate Chinese_PRC_CS_AS_WS 
    from @t
    group by content  collate Chinese_PRC_CS_AS_WS