我有3张桌子:
Table t1: Table t2: Table t3:
+----+----+ +----+----+ +----+----+
| i1 | i2 | | i2 | c2 | | i1 | c1 |
+----+----+ +----+----+ +----+----+
| 1 | 22 | | 11| cc | | 1 | e |
| 2 | 33 | | 22| bb | | 2 | f |
| 3 | 11 | | 33| aa | | 3 | g |
+----+----+ +----+----+ +----+----+
我希望使用表t1和列i1和i2从t2和c3中选择colum c2,结果将是thins:
requested Tabel:
+----+----+
| c1 | c2 |
+----+----+
| e | bb |
| f | aa |
| g | cc |
+----+----+
我选择了一个像这样的联盟:
select pve.c1
from dbo.t3 as pve
join dbo.t1 as v
on pve.i1 = v.i1
union
select vse.c2
from dbo.t2 as vse
join dbo.t1 as e
on vse.i2 = e.i2
结果如下:
resulted Tabel:
+----+
| i1 |
+----+
| e |
| f |
| g |
| bb|
| aa|
| cc|
+----+
谢谢!
答案 0 :(得分:6)
试试这个,
SELECT c.c1, b.c2
FROM t1 a
INNER JOIN t2 b
on a.i2 = b.i2
INNER JOIN t3 c
on a.i1 = c.i1
答案 1 :(得分:3)
SELECT c1, c2 FROM t1
JOIN t2 ON t1.i2=t2.i2
JOIN t3 ON t1.i1=t3.i1;