表product:
ID  Name  Category  
1    a      A
2    b      A
3    c      B
4    d      B
5    e      C
6    f      C现在希望从Category字段进行分类并选出一条记录显示出来,,
ID  Name  Category  
1    a      A
3    c      B
5    e      C请高手指教!

解决方案 »

  1.   

    select * from product a where not exists(select * from product b where a.Category=b.Category and b.id>a.id   )
      

  2.   

    select * from product a where id=(select min(id) from product where category=a.category)
      

  3.   

    select * from product a where 
    not exists(select * from product b where a.Category=b.Category and b.id<a.id)
      

  4.   

    create table product(
      ID int identity(1,1),
      Name char(1),
      Category char(1)
    )insert product
    select 'a','A' union all
    select 'b','A' union all
    select 'c','B' union all
    select 'd','B' union all
    select 'e','C' union all
    select 'f','C'--第一种方法:
    select * from product
    select * from product a where ID in(select min(ID) from product where Category=a.Category) --第二种方法:
    select a.* from product a,
    (select Category,min(ID)as ID from product group by Category)b
    where a.Category=b.Category and a.ID=b.ID--结果:
    ID       Name     Category
    --------------------------
    1 a A
    3 c B
    5 e C
      

  5.   

    select * from product a where 
     exists(select * from product b where a.Category=b.Category and a.id<b.id)
      

  6.   

    select * from product 
    where id not in (select max(id) 
                     from product
                     group by Category)
      

  7.   


    select * from product where id in (select min(id) from product group by category)
      

  8.   

    同意 kourr2004(迷茫的战斗鹅) select * from product a where ID in(select min(ID) from product where Category=a.Category) --第二种方法:
    select a.* from product a,
    (select Category,min(ID)as ID from product group by Category)b
    where a.Category=b.Category
      

  9.   

    create table product(
      ID int identity(1,1),
      Name char(1),
      Category char(1)
    )insert product
    select 'a','A' union all
    select 'b','A' union all
    select 'c','B' union all
    select 'd','B' union all
    select 'e','C' union all
    select 'f','C'select min(id),min(name),Category from product group by Category