我在同一个 SQL Server 2008 R2 数据库中有 6 个表(t1、t2、t3、...、t6。我想将它们分成两组。第 1 组(TG1)包括表 1、2 和 3。第 1 组(TG1 ) 包括表 4、5 和 6。我创建了两个查询,每个查询都使用 union
SELECT TG1.*
FROM (SELECT t1.PID AS ID, t1.FVALUE AS FVALUE, t1.TVALUE AS TVALUE
FROM mydb.myuser.t1
UNION
SELECT t2.PID AS ID, t2.FVALUE AS FVALUE, t2.TVALUE AS TVALUE
FROM mydb.myuser.t2
UNION
SELECT t3.PID AS ID, t3.FVALUE AS FVALUE, t3.TVALUE AS TVALUE
FROM mydb.myuser.t3) AS TG1
SELECT TG2.*
FROM (SELECT t4.PID AS PID, t4.VALUE AS VALUE, t4.VALUEID AS VALUEID, t4.VALUEDESC AS VALUEDESC
FROM mydb.myuser.T4
SELECT t4.PID AS PID, t4.VALUE AS VALUE, t4.VALUEID AS VALUEID, t4.VALUEDESC AS VALUEDESC
FROM mydb.myuser.T4
SELECT t4.PID AS PID, t4.VALUE AS VALUE, t4.VALUEID AS VALUEID, t4.VALUEDESC AS VALUEDESC
FROM mydb.myuser.T4) AS TG2
我得到以下两个表
我要创建的最终查询如下面的屏幕截图所示,获取 TG1 的所有值并填充 TG2 中与 TG1 的值匹配的其他字段。
上面屏幕截图中的值是使用 vlookup 在 Excel 中填充的,我需要帮助才能在 SQL 查询中执行相同的操作。我感谢您的帮助。
查看: