简体   繁体   中英

Sql table query for tables with no direct link

I have Table_A that has a column: 'FullName',columnB, .. and other columns

I have Table_B that has columns: 'FirstName', 'LastName','Job_OID'

Evidently, If we join the characters of FirstName and LastName , it matches Full_Name .

This is the only thing common between these two tables!

Problem: When the Full Name is "John Smith" in Table_A, I want to fetch his Job_OID from table_B.

In simple language, select job_oid from table_B where ('FirstName' + 'LastName') = Table_A.FullName;

try this:

SELECT job_oid 
FROM table_B 
JOIN Table_A ON Table_A.FullName = Table_B.FirstName+' '+TableB.LastName

You can also write query in this way:

SELECT B.job_oid 
FROM Table_A  A
INNER JOIN table_B  B ON A.FullName = CONCAT(B.FirstName, ' ', B.LastName)

I think, you must normalize your DB, but if it isn't possible, you can write following query:

SELECT B.*
FROM Table_B B
WHERE EXISTS
    (SELECT 'PERSON'
    FROM Table_A A
    WHERE B.FirstName + ' ' + B.LastName = A.FullName)

You can use, otherwise, JOIN command like this:

SELECT B.*
FROM Table_B B
JOIN Table_A A
    ON B.FirstName + ' ' + B.LastName = A.FullName

So, in this way you link two tables with a concatenation of two fields (FirstName and LastName of Table_B) with FullName stored in Table_A

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM