数据库中有这样一个表  table,字段列表是
  ID     Name    CateGory
  1      张三    学生
  2      李四    教师
  3      王五    办公人员
  4      找六    教师
  5      周七     学生现在我想把这些数据中,category不同的数据筛选出一条来,比如 “学生” 类别现在有 1 5两条记录,我可以选出1 也可以选5,这个无所谓,但是只要是“学生”类别,只选一条,“教师”类别也是一样。这里是简单的例子,数据库中的记录很多,如何写sql语句或者存储过程?谢谢各位.另外我只有45分了,全部在这里,分数少,但是心意在,请各位不要嫌弃

解决方案 »

  1.   

    SELECT * FROM TB T WHERE ID=(SELECT MIN(ID) FROM TB WHERE CATEGORY=T.CATEGORY)
      

  2.   

    select *
    from tb t
    where not exists(select 1 from tb where CateGory=t.CateGory and id<t.id)
      

  3.   

    ID不重复的话
    SELECT * FROM TB T1
    WHERE NOT EXISTS(SELECT 1 FROM TB T2 WHERE T2.CATEGORY=T1.CATEGORY AND T2.ID<T1.ID)
      

  4.   


    select t.* from tb t where id = (select min(id) from tb where CateGory = t.CateGory) order by t.idselect t.* from tb t where id = (select max(id) from tb where CateGory = t.CateGory) order by t.id
      

  5.   

    ---测试数据---
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([ID] int,[Name] varchar(4),[CateGory] varchar(8))
    insert [tb]
    select 1,'张三','学生' union all
    select 2,'李四','教师' union all
    select 3,'王五','办公人员' union all
    select 4,'找六','教师' union all
    select 5,'周七','学生'
     
    ---查询---
    select *
    from tb t
    where not exists(select 1 from tb where CateGory=t.CateGory and id<t.id)
    ---结果---
    ID          Name CateGory 
    ----------- ---- -------- 
    1           张三   学生
    2           李四   教师
    3           王五   办公人员(所影响的行数为 3 行)
      

  6.   

    --相同CateGory取最小id 
    select t.* from tb t where id = (select min(id) from tb where CateGory = t.CateGory) order by t.id--相同CateGory取最大id 
    select t.* from tb t where id = (select max(id) from tb where CateGory = t.CateGory) order by t.id--相同CateGory随机取id 
    select t.* from tb t where id = (select top 1 id from tb where CateGory = t.CateGory order by newid()) order by t.id
      

  7.   

    select
      * 
    from
      [table] t
    where
      id=(select min(id) from [table] where CateGory=t.CateGory)
      

  8.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-12-17 15:40:44
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go 
    create table [tb]([ID] int,[Name] varchar(4),[CateGory] varchar(8))
    insert [tb]
    select 1,'张三','学生' union all
    select 2,'李四','教师' union all
    select 3,'王五','办公人员' union all
    select 4,'找六','教师' union all
    select 5,'周七','学生'
    --------------开始查询--------------------------
    select
      * 
    from
      tb t
    where
      id=(select min(id) from tb where CateGory=t.CateGory)
    ----------------结果----------------------------
    /* ID          Name CateGory
    ----------- ---- --------
    3           王五   办公人员
    2           李四   教师
    1           张三   学生(3 行受影响)*/
      

  9.   


    请问 tb t 是什么意思?是不是table的意思,那where CateGory=t.CateGory有什么作用?如果不用前面的union,能否同样实现,,现在是不用前面的union语句我这里还是筛选不出来
      

  10.   

    TB 是表名,T是别名,后面是连接条件