I have three tables:
Profile -ProfileID -FirstName -LastName ProfilePhoneNumber -ProfileID -PhoneNumberID PhoneNumber -PhoneNumberID -PhoneNumberTypeID -Number ProfilePhoneNumber is a simple bridge table between Profile and PhoneNumber.
I want to query for specific phone number types and return a single row. I want to be able to accept null values because not all people will have all types of phone numbers.
Here is my current query:
SELECT p.FirstName , p.LastName , bpn.Number as BusinessPhoneNumber , mpn.Number as MobilePhoneNumber FROM Profile p LEFT JOIN ProfilePhoneNumber ppn ON p.ProfileID = ppn.ProfileID LEFT JOIN PhoneNumber bpn ON ppn.PhoneNumberID = bpn.PhoneNumberID AND bpn.PhoneNumberTypeID = '1' LEFT JOIN PhoneNumber mpn ON ppn.PhoneNumberID = mpn.PhoneNumberID AND mpn.PhoneNumberTypeID = '2' WHERE p.ProfileID = '123' This always works, but returns three rows because Profile 123 has three phone numbers, and so the query returns a row for each phone number.
If I change it to an INNER JOIN on PhoneNumber, I can get only one row back, but only in circumstances where the Profile being queried has all of the PhoneNumberTypeID types that I am querying for.
How do I return one row that is null tolerant?
Select distinctsolve your problem? If not, how do you want to choose which of the business or mobile phone numbers to display, when someone has more than one?