Reputation: 1344
I would like to know how to compare two database table records and return the results into separate table.
For example, I have a total of 6 columns in each table A B C D E F. However, I'm only comparing on the records in columns A, B, & C
If there is a match in in each table of columns A B C I would like the results to be outputted to new table (.csv file etc) for the row that matched
Let me illustrate:
In Table A you can see 6 columns, A B C D E F. The table has the following rows:
Table A
Table B also has 6 columns A B C D E F
From the tables you can see that only one row has match on A B & C i.e xx, PP, dd.
I would like that row to be outputted to a table with all the records in that row ie. xx PP dd TYH ADF AD
Upvotes: 0
Views: 174
Reputation: 27289
Far from being complicated this is SQL bread and butter... you are looking for a join condition e.g.
select A.*, B.*
from TableA A
inner join TableB B on A.A = B.A and A.B = B.B and A.C = B.C
where A.* is all the columns from table A (TableA) and B.* is all the columns from table B (TableB)
Edit: Based on your clarified requirement for a separate row from tables A & B, try the following:
select convert(bit,1) Is_Deleted, A.A, A.B, A.C, A.D, A.E, A.F
from TableA A
inner join TableB B on B.A = A.A and B.B = A.B and B.C = A.C
union all
select convert(bit,1) Is_Deleted, B.A, B.B, B.C, B.D, B.E, B.F
from TableB B
inner join TableA A on A.A = B.A and A.B = B.B and A.C = B.C
order by A, B, C
Upvotes: 2
Reputation: 40517
Not clear if you want the output from table 1 or table 2. Though the query will be quite simple.
SELECT t.A, t.B, t.C, t.D, t.E, t.F
FROM TableA t
INNER JOIN TableB tt ON (t.A = tt.A AND t.B = tt.B AND t.C = tt.C)
Upvotes: 2