gpt4 book ai didi

sql - 从 3 个表中选择 2 列

转载 作者:行者123 更新时间:2023-12-02 12:05:09 25 4
gpt4 key购买 nike

我有 3 张 table :

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 |
+----+----+ +----+----+ +----+----+

我想使用带有 i1 和 i2 列的表 t1 从 t2 中选择列 c2,从 t3 中选择 c1 列,结果会很薄:

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|
+----+

谢谢!

最佳答案

试试这个,

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

SQLFiddle Demo

关于sql - 从 3 个表中选择 2 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12051111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com