I faced a situation where I got duplicate values from LEFT JOIN
. I think this might be a desired behavior but unlike from what I want.
I have three tables: person
, department
and contact
.
person :
id bigint,
person_name character varying(255)
department :
person_id bigint,
department_name character varying(255)
contact :
person_id bigint,
phone_number character varying(255)
Sql Query :
SELECT p.id, p.person_name, d.department_name, c.phone_number
FROM person p
LEFT JOIN department d
ON p.id = d.person_id
LEFT JOIN contact c
ON p.id = c.person_id;
Result :
id|person_name|department_name|phone_number
--+-----------+---------------+------------
1 |"John" |"Finance" |"023451"
1 |"John" |"Finance" |"99478"
1 |"John" |"Finance" |"67890"
1 |"John" |"Marketing" |"023451"
1 |"John" |"Marketing" |"99478"
1 |"John" |"Marketing" |"67890"
2 |"Barbara" |"Finance" |""
3 |"Michelle" |"" |"005634"
I know it's what joins do, keeping multiplied with selected rows. But It gives a sense like phone numbers 023451
,99478
,67890
are for both departments while they are only related to person john with unnecessary repeated values which will escalate the problem with larger data set.
So, here is what I want:
id|person_name|department_name|phone_number
--+-----------+---------------+------------
1 |"John" |"Finance" |"023451"
1 |"John" |"Marketing" |"99478"
1 |"John" |"" |"67890"
2 |"Barbara" |"Finance" |""
3 |"Michelle" |"" |"005634"
This is a sample of my situation and I am using a large set of tables and queries. So, kind of need a generic solution.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…