我一直在研究这个问题已经有一段时间了,我无法弄清楚要做什么。
我有一个包含多个连接的巨大SQL查询,它为我提供了数十万条记录,这是完美的。
然后我意识到我在另一个表中有200多个记录需要添加到第一个表中。
第一张表:
Field1 Field2 Field3 john smith 23 Boston Mohammed Ali 45 New York Stephanie Johnson 15 Los Angeles
新表
Field1 OtherField1 OtherField2 Mark Khoury Null null
所以我真的只需要将表2中的Field1值添加到第一个的“底部”。我在第一个查询中创建的所有连接也应该适用于表2中的值。
联盟不起作用,因为我只在列上添加。我必须从第一个表中复制粘贴相同的查询,以从第二个表的值中获取“Field2”和“Field3”。
我希望它看起来像是:
Field1 Field2 Field3 John Smith 23 Boston Mohammed Ali 45 New York Stephanie Johnson 15 Los Angeles Mark Khoury 65 Houston
我该怎么做呢?我不想“加入”表格,我想将它们联合起来但只有一列。
这是我的意思的一个例子:
表一是通过执行以下操作创建的:
SELECT table.value1 as Field1, table2.value2 as Field2, table3.value3 as Field3
FROM some_table as table
LEFT OUTER JOIN some_other_table as Table2 ON table.field = table2.field5
LEFT OUTER JOIN a_third_table as table3 ON table2.field2 = table3.field4
但是现在我有了更多Field1值的newTable,我需要将这些值添加到第一个表中。
我试过了:
SELECT COALESCE(table.value1, NewTable.value) as Field1, table2.value2 as Field2,
table3.value3 as Field3
FROM some_table as table
LEFT OUTER JOIN some_other_table as Table2 ON table.field = table2.field5
LEFT OUTER JOIN a_third_table as table3 ON table2.field2 = table3.field4, newTable
但这给了我一定数量的结果,它应该给我几百个。
答案 0 :(得分:4)
您可以在第一列添加联合,并将其他列设置为“类似
”SELECT Field1, Field2
FROM table1
UNION
SELECT Field1, '' AS Field2
FROM table2
那会得到你所需要的吗?
举个例子,以下是否能满足您的需求?
SELECT table.value1 as Field1, table2.value2 as Field2, table3.value3 as Field3
FROM
(SELECT t.value1 as Field1 FROM some_table as t
UNION
SELECT t2.value1 as Field1 FROM some_table2 as t2) as table
LEFT OUTER JOIN some_other_table as Table2 ON table.field1 = table2.field5
LEFT OUTER JOIN a_third_table as table3 ON table2.field2 = table3.field4
答案 1 :(得分:0)
与@rhollyer有相同的想法,这里有一个脚本,可以产生你要求的东西。
DECLARE @Person TABLE
(
PersonID int,
DisplayName nvarchar(50)
)
DECLARE @PersonDetail TABLE
(
PersonID int,
Age int
)
DECLARE @PersonGeoInfo TABLE
(
PersonID int,
City nvarchar(200)
)
DECLARE @LostPersons TABLE
(
LostPersonID int,
DisplayName nvarchar(50),
Irrelevant1 decimal,
Irrelevant2 bit
)
INSERT INTO @Person (PersonID, DisplayName)
VALUES
(1, 'John Smith'),
(2, 'Mohammed Ali'),
(3, 'Stephanie Johnson')
INSERT INTO @PersonDetail(PersonID, Age)
VALUES
(1, 23),
(2, 45),
(3, 15),
(4, 65)
INSERT INTO @PersonGeoInfo(PersonID, City)
VALUES
(1, 'Boston'),
(2, 'New York'),
(3, 'Los Angeles'),
(4, 'Houston')
INSERT INTO @LostPersons(LostPersonID, DisplayName, Irrelevant1, Irrelevant2)
VALUES
(4, 'Mark Khoury', 9.5, 1)
DECLARE @Person TABLE
(
PersonID int,
DisplayName nvarchar(50)
)
DECLARE @PersonDetail TABLE
(
PersonID int,
Age int
)
DECLARE @PersonGeoInfo TABLE
(
PersonID int,
City nvarchar(200)
)
DECLARE @LostPersons TABLE
(
LostPersonID int,
DisplayName nvarchar(50),
Irrelevant1 decimal,
Irrelevant2 bit
)
INSERT INTO @Person (PersonID, DisplayName)
VALUES
(1, 'John Smith'),
(2, 'Mohammed Ali'),
(3, 'Stephanie Johnson')
INSERT INTO @PersonDetail(PersonID, Age)
VALUES
(1, 23),
(2, 45),
(3, 15),
(4, 65)
INSERT INTO @PersonGeoInfo(PersonID, City)
VALUES
(1, 'Boston'),
(2, 'New York'),
(3, 'Los Angeles'),
(4, 'Houston')
INSERT INTO @LostPersons(LostPersonID, DisplayName, Irrelevant1, Irrelevant2)
VALUES
(4, 'Mark Khoury', 9.5, 1)
SELECT P.DisplayName AS Field1, PD.Age AS Field2, PGI.City AS Field3
FROM (
SELECT P.PersonID AS PersonID, P.DisplayName AS DisplayName FROM @Person P
UNION
SELECT LP.LostPersonID AS PersonID, LP.DisplayName AS DisplayName FROM @LostPersons LP) AS P
LEFT OUTER JOIN @PersonDetail PD ON P.PersonID = PD.PersonID
LEFT OUTER JOIN @PersonGeoInfo PGI ON PD.PersonID = PGI.PersonID