I am trying to select places infos from different tables: I have the place in one table, the geographical coordinates on another, and telephone and email in another. The issue is that we may or not have the email and telephone. I am looking for a way to get the email / phone when they exist, and have a blank value if they are not known. So I use the "CASE" instruction on the SELECT.
If we have no email and no phone, everything goes ok : we have only one line returned.
But if we have the email or the phone, it returns 2 lines; and the worst case is when we have both email and phones: in this case it returns three lines for each place :
- place name , blank phone , blank mail
- place name , blank phone , populated mail
- place name , populated phone , blank mail
I am looking for a way to get only one line :
0) place name + populated (or not) phone + populated (or not) mail
I tried the 'distinct' but it doesn't do the trick :(
Here is my query :
select distinct
places.name as place,
CASE place_properties.pkey WHEN 'gen.telephone'
THEN place_properties.pvalue
END
as telephone,
CASE place_properties.pkey WHEN 'gen.email'
THEN place_properties.pvalue
END
as email
from
places
inner join
place_properties on place_properties.place_id = places.id
And here is a simplified answer example :
"ALLIAT" ;"0561059858" ;""
"ALLIAT" ;"" ;"[email protected]"
"ALLIAT" ;"" ;""
"TARASCON SUR ARIEGE" ;"0561023281" ;""
"TARASCON SUR ARIEGE" ;"" ;"[email protected]"
"TARASCON SUR ARIEGE" ;"" ;""
"Les Cabanes" ;"" ;""
We see that 'ALLIAT' and 'Tarascon' are returned three time because it has both a phone number and an email while "Les cabanes" is returned only once because it doesn't have any of it.
How to change the SQL to have only one line when we habe the email and/or the phone on the database?