比如两个表TB_Class(班级)、TB_Student(学生)
TB_Class中两列:
 ID Name;
 1  高三一班
 2  高三二班TB_Student表中五列:
 ID Class_ID Name Age Sex
 1   1       张三 16  男
 2   1       李四 17  女
 3   2       王五 18  女现在如何从TB_Student中选择每一个班里挑出年龄最小的那一名学生的所有情况?
group by 的话好像不行吧。----------------------
沙发10分。

解决方案 »

  1.   

    select *  from(select min(id) over(partition by entity_id) m,s.* from s_entity_attribute s)x where x.m=x.id 
    类似这样用分析函数试试
    按班分组后查出最小的在关联查班名
      

  2.   

    以下2个那个快?那个好理解?
    ---------------------
    select t1.*
    from TB_Student t1,
    (select Class_ID,  mix(Age) mixAge
    from TB_Student
    group by Class_ID) t2
    where t2.Class_ID=t1.Class_ID
    and t2.mixAge=t1.Age
    ----------------------------
    select *
    from TB_Student t1
    where t1.Age=(  SELECT Min(Age) FROM TB_Student t2 WHERE t1.Class_ID = t2.Class_ID )
    ----------------------------
      

  3.   


    select * from TB_Student t where (t.Class_ID,t.age) 
    in (select Class_ID,min(age) from TB_Student group by Class_ID);
      

  4.   

    如果有两个是最小年龄的,但只要一个呢(任一个)。用distinct?
      

  5.   

    如果有两个是最小年龄的,但只要一个呢(任一个)。用distinct?
    要是同班里最小的那几个Name,Age,Sex都相同呢?
    select distinct t1.Class_ID,Name,Age,Sex 出来就没意义了
    ---------------------
    select distinct t1.Class_ID,t1.Name,t1.Age,t1.Sex
    from TB_Student t1,
    (select Class_ID,  mix(Age) mixAge
    from TB_Student
    group by Class_ID) t2
    where t2.Class_ID=t1.Class_ID
    and t2.mixAge=t1.Age
    ----------------------------
    select distinct t1.Class_ID,Name,Age,Sex
    from TB_Student t1
    where t1.Age=(  SELECT Min(Age) FROM TB_Student t2 WHERE t1.Class_ID = t2.Class_ID )