SQL Join two table with key into different column

MrTex

I have two table, in SQL; the first table(T1) is this:

ID  
----
401 
402
403
634
635
636

The second table(T2) is this:

ID1 | VALUE1 | ID2 | VALUE2
---- -------- ----- -------
401 | TEST   | 634 | SAMPLE1
402 | MYVAL  | 635 | TRUE

The result i need is this:

T1.ID | T2.ID1| T2.VALUE1 | T2.ID2 | T2.VALUE2
------ ------- ----------- -------- ----------
401   | 401   | TEST      | 634    | SAMPLE1
402   | 402   | MYVAL     | 635    | TRUE
634   | 401   | TEST      | 634    | SAMPLE1
635   | 402   | MYVAL     | 635    | TRUE

The value 403 and 636 of T1 must not be present in the result because don't have any reference in T2.

There is a way to accomplish this with some INNER JOIN? I'm using MS SQL Server 2017.

JohnMaxwell

Suppose you can do simple join tables like this:

select t1.id, t2.id1, t2.value1, t2.id2, t2.value2
from t1
join t2 on (t1.id = t2.id1 or t1.id = t2.id2)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

join two sql table as column

Join two table with two different Foreign Key for get same field

join two table, input and order by two different column

How to join two dataframes with different key column names in pydatatable?

Join on two table with same primary key but different data

Join SQL table using the same row but different view/column output

MYSQL join two different columns from two different table as single column

SQL join two tables with no key column but need to join with date filter column

Join two Python dictionaries based on same value but different key name (like SQL's JOIN)

sql server compare two column from different table

SQL - compares column values from two different table

Is it possible to display other column names from two table to one table by using join clause by using foreign key

sql join with foreign key table

SQL join table on a modified key

Join table two column as 1 column

How to name two columns in join tables with two same foreign key at one table in SQL?

How to join SQL statements with different where clause on different column of same table

How to join two file with a key column in awk

How can i join two tables with two columns that refers the same column in the second table in SQL Server

Join two tables based on different column type

inner join two foreign key in one table

join two columns with same title but in different table

SQL Join a row table with a column table

How can I return two different values from the same column in a SQL Join?

how to join two table with where to one column

How to join table based on two column in mysql?

MySql Join two table with column names

SQL join two tables without unique key

Can I join two Select sum SQL queries from the same table but with different filters?