I have 3 tables with different columns except for one called "FileKey". I need a query that returns that field of the 3 tables as a list I think here is more easy to see
Please be patient, i'm new here :(
I have 3 tables with different columns except for one called "FileKey". I need a query that returns that field of the 3 tables as a list I think here is more easy to see
Please be patient, i'm new here :(
You can do this with a simple JOIN of the three tables ON the FileKey value.
you can use union like this:
Select FieldKey from TableA
union
Select FieldKey from TableB
union
Select FieldKey from TableC
Using union it will not duplicate data if you have the same FieldKey in diferents tables.
if you want to duplicate data use union all like this:
Select FieldKey from TableA
union all
Select FieldKey from TableB
union all
Select FieldKey from TableC
check this link below to understand it better https://www.w3schools.com/sql/sql_union.asp
The best possible way to do this is by using UNION Operator. but if you don't want to use the UNION what you can do is to insert your all three table data to a temporary table and later on view it using a Select Statement.
DECLARE @TableA TABLE (ID INT);
DECLARE @TableB TABLE (ID INT);
DECLARE @TableC TABLE (ID INT);
DECLARE @Needed TABLE (ID INT);
INSERT INTO @TableA VALUES(53)
INSERT INTO @TableB VALUES(49)
INSERT INTO @TableC VALUES(34)
INSERT INTO @Needed (Id)
SELECT ID FROM @TableA
INSERT INTO @Needed (Id)
SELECT ID FROM @TableB
INSERT INTO @Needed (Id)
SELECT ID FROM @TableC
SELECT * FROM @Needed