假设
表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:
--------------------------
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
--------------------------
高手请帮忙
李四取30而不取10,20,40?????
-------------------
取多少都可以,其实我想,能不能在左连接,右连接中用TOP 1之类的东西来去第一条
from 表1
from table1
from table1
select id,xingming,nianling,(select max(file) from table2 where id=table1.id )
from table1
大家明白我的意思吗?,两个表中都有唯一标示的字段,但是只有ID与ID有连接关系
select id,xingming,nianling,(select top 1 file from table2 where id=table1.id order by newid()) as xxx
from table1
这样总行了吧,你试过没?
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