简体   繁体   中英

SQL join query - selecting from two tables

I have a query which checks a table for a number of fields. The two tables I am interested in are: PERSON & PERSON_ALTERNATE_ID.

I want to modify my query to also return the value stored in person_alternate_id (if the particular person indeed has one)

select distinct person.person_id, person_name, person_address
from person join person_alternate_id
on
person.person_id=person_alternate_id.person_id
where person.person_id
in (10001,10002,10003);

Can anyone suggest how I could do that? I was looking at nested select examples, but I wasn't able to implement a suitable change to my query that achieved what I require. At the moment, the query only returns fields I need from the PERSON table.

Because the person may or may not have an alternate id, you should use a left join:

select person.person_id, person_name, person_address, person_alternate_id.*
from person 
left join person_alternate_id
on person.person_id=person_alternate_id.person_id
where person.person_id
in (10001,10002,10003);

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