I have two tables where I’m trying to select rows where a common field between those tables matches exactly, however it’s proving difficult to write the query. Here is a simplified version:
The tables look like this (simplified):
T1: id, name, sn T2: id, location, sn
I’m trying to get t1.name and t2.loc together only where t1.sn=t2.sn. The sn field is unique in both, and so at the most, only 1 record will match between tables. In t1, all records have a sn field value, however in t2 about 30% of them have NULL for sn. So, I an expecting the join to produce somewhat fewer rows than t1 has.
How would I do the join?
Thanks.
Sample data:
t1: +---+--------+-------+-----+ | id| name | sn | ... | +---+--------+-------+-----+ | 1 | thing1 | 12345 | | | 2 | thing2 | 10000 | | | 3 | thing3 | 33445 | | | 4 | thing4 | 99223 | | +---+--------+-------+-----+ T2: +----+--------+-------+-----+ | id | loc | sn | ... | +----+--------+-------+-----+ | 90 | here | 12345 | | | 92 | there | NULL | | | 96 | near | 33445 | | | 99 | far | 99223 | | +----+--------+-------+-----+ Result: +--------+-------+-------+ | name | loc | sn | +--------+-------+-------+ | thing1 | here | 12345 | | thing3 | near | 33445 | | thing4 | far | 99223 | +--------+-------+-------+
1 Answer