举列:
table1结构
 学号 姓名(学号是主键)
 2001 张三
 2002 李四
 2003 王五
table2结构(没有主键)
 学号 分数
 2001 77
 2001 88
 2002 99
 2003 66 
 2003 55
想要得出结果如下:
 学号  姓名  分数
 2001  张三   77
 2002  李四   99
 2003  王五   66
谢谢了.

解决方案 »

  1.   

    table2里面有重复的,最终结果选哪个呢?最大?select table1.学号,table1.姓名,max(table2.成绩)
    from table1 inner join table2 on tabl1.学号=table2.学号
      

  2.   

    select t1.学号,t1.姓名,t2.分数
    from table1 t1,(select 学号,分数 = (select distinct min(分数) from table2 where 学号 = t3.学号) from table2 t3) t2
    where t1.学号 = t2.学号
      

  3.   

    --修改一下
    select t1.学号,t1.姓名,t2.分数
    from table1 t1,(select 学号,分数 = (select distinct min(分数) from table2 where 学号 = t3.学号 group by 学号) from table2 t3) t2
    where t1.学号 = t2.学号
      

  4.   

    看你的效果,應該是隨便得到一個分數即可Select A.*,B.分数
    From table1 A
    Inner Join
    (Select 学号,Max(分数) As 分数 From table2 Group By 学号 ) B
    On A.学号=B.学号ORSelect A.*,B.分数
    From table1 A
    Inner Join
    (Select 学号,Min(分数) As 分数 From table2 Group By 学号 ) B
    On A.学号=B.学号
      

  5.   

    iamltd(妖)              出错,不能运行
    coolingpipe(冷箫轻笛)   得出的结果不一样(不是三条记录,有重复)
    paoluo(一天到晚游泳的鱼) 结果差不多了,只是我不要最大值,也不要最小值,
     2001  张三   77
     2003  王五   66
    这两条记录可以看出来,他们只是按顺序得到的,谁在前面就用谁的分数.
    谢谢你们.谢谢.
      

  6.   

    如果是要得到第一個,就麻煩些,要借用臨時表.
    --建立測試環境
    Create Table table1
    (学号 Char(4),
     姓名 Nvarchar(10))Create Table table2
    (学号 Char(4),
     分数 Int)
    --插入數據
    Insert table1 Select '2001', N'张三'
    Union All Select '2002', N'李四'
    Union All Select '2003', N'王五'
    Insert table2 Select '2001', 77
    Union All Select '2001', 88
    Union All Select '2002', 99
    Union All Select '2003', 66 
    Union All Select '2003', 55
    GO
    --測試
    Select ID=Identity(Int,1,1),* Into #T From table2Select A.*,B.分数
    From table1 A
    Inner Join
    (Select * From #T Where ID In (Select Min(ID) From #T Group By 学号)) B
    On A.学号=B.学号
    GO
    --刪除測試環境
    Drop Table table1,table2,#T
    GO
    --結果
    /*
    学号 姓名 分数
    2001 张三 77
    2002 李四 99
    2003 王五 66
    */
      

  7.   

    paoluo(一天到晚游泳的鱼) 谢谢你.
    如果不用临时表就更好了,呵呵..
      

  8.   

    paoluo(一天到晚游泳的鱼) ( ) 信誉:100  2006-5-31 10:01:57  得分: 0  
    ??我要怎样给你分数?