Firstly, as you have been told multiple times in the comments, this is working exactly as you have written, and (more importantly) as intended. You have 2 rows in tableA
and those 2 rows match 2 rows in your table tableB
according to the ON
clause. This means that each join operation, for the each of the rows in tableA
, results in 2 rows as well; thus 4 rows (2 * 2 = 4).
Considering that your table, TableA
only has one column then it seems that you should be cleaning up that data and deleting the duplicates. There are plenty of examples on how to do that already (example).
Perhaps the column you show us in TableA
is one many, and thus instead you have a denormalisation issue, and instead there should be another table with the details of Id_trans
and a PRIMARY KEY
or UNIQUE CONSTRAINT/INDEX
on it. Then you would join fron that table to TableB
.
Finally, what you might be after is an EXISTS
, which would look like this:
SELECT B.trans_id, B.[name]
FROM dbo.TableB B
WHERE EXISTS(SELECT 1
FROM dbo.TableA A
WHERE A.ID_Trans = B.trans_id); --Odd that it's called ID_Trans in one table, and Trans_ID in another