Conditional join Postgresql

Viewed 25

I have a table that includes some information about each time a user uses mobile or web app ie

id platform
1 web
1 mobile
2 web
2 web

I want to join another table with this table which says what each user uses as a phone_model

id phone_model
1 iphone
2 android

But I want to conditionally join these so that only the mobile entries show the phone_model and the web row to be kept empty, ie only join that information in cases where platform = 'mobile'

id platform phone_model
1 web
1 mobile Iphone
2 web
2 web

How can I do this?

1 Answers

You seem to want a left join:

select t1.*, t2.phone_model
from table1 t1 left join
     table2 t2
     on t2.id = t1.id and t1.platform = 'mobile';
Related