假设
表1:
--------------------------
 id  xingming  nianling 
 01    张三       20 
 02    张四       21   
 03    卡卡       24
 04    狒狒       11
 05    毫毫       15
--------------------------
表2:
--------------------------
 id    file 
 01     20
 01     21
 01     11
 02     10
 02     20
 02     30
 02     40
--------------------------
表1与表2通过id关联
想要的数据是这样的
--------------------------
 id  xingming  nianling  file
 01    张三       20      20
 02    张四       21      30
 03    卡卡       24      null
 04    狒狒       11      null
 05    毫毫       15      null
--------------------------
也不知道是怎么搞的左连接,右连接,内连接多不行,得到的都是以下之类的
--------------------------
 id  xingming  nianling  file
 01    张三       20      20
 01    张三       20      21
 01    张三       20      11
 02    张四       21      30
 02    张四       21      20
 02    张四       21      30
 02    张四       21      40
 03    卡卡       24      null
 04    狒狒       11      null
 05    毫毫       15      null
--------------------------
高手请帮忙

解决方案 »

  1.   

    你的file表中一个ID对应了多个 file中,你要的结果中,为什么张三就取20而不取21,
    李四取30而不取10,20,40?????
      

  2.   

    cpp2017(幕白兄) 
    -------------------
    取多少都可以,其实我想,能不能在左连接,右连接中用TOP 1之类的东西来去第一条
      

  3.   

    即然是任取.可以这样select id  xingming  nianling ,(select top 1 file from 表2 where id= 表1.id)
    from 表1
      

  4.   

    select id,xingming,nianling,(select max(file) from table2 where id=id )
    from table1
      

  5.   

    select id,xingming,nianling,(select max(file) from table2 where table1.id=table2.id ) as file
    from table1
      

  6.   

    少了
    select id,xingming,nianling,(select max(file) from table2 where id=table1.id )
    from table1
      

  7.   

    其实我的想法很简单,两个表关联,第一个关联第二个表,第二个表中,可能有多条,也可能没有符合条件的,即(表1ID=表2ID)的,如果有多条就去第一条与第一个表关联,如果没有就要像左连接那样,列出第二个表中的空字段既(05    毫毫       15      null)----------------------
    大家明白我的意思吗?,两个表中都有唯一标示的字段,但是只有ID与ID有连接关系
      

  8.   

    随机的
    select id,xingming,nianling,(select top 1 file from table2 where id=table1.id order by newid()) as xxx
    from table1
    这样总行了吧,你试过没?
      

  9.   

    看了你的获得结果,file列获得的是表2中第二大的数值,故语句如下:
    select id,xingming,nianling,(select top 1 from (select top 2 file from table2 where table1.id=table2.id order by file desc) order by file) as file from table1