简体   繁体   中英

How to cross reference two tables that have FirstName + LastName fields

I want to match two tables that both have first + last name fields. I would like to return the records that match each person's first + last name combination.

Table 1 fields:

id|firstname|lastname|position|

Table 2 fields:

firstname|lastname|datehired|department|deptcode|

You can join on multiple columns:

SELECT t1.id, t1.firstname, t1.lastname, t1.position, 
       t2.datehired, t2.department, t2.deptcode
FROM Table1 t1 INNER JOIN Table2 t2
    ON t1.firstname = t2.firstname
   AND t1.lastname  = t2.lastname

How about:

Select FirstName, LastName From Table1

Intersect

Select Firstname, LastName From Table2

You need to include both conditions in your join

SELECT *  
FROM Table1  
JOIN Table2 ON Table1.firstname = Table2.firstname AND Table1.lastname = Table2.lastname

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