L
L
Lavrov952018-11-19 02:11:29
SQL
Lavrov95, 2018-11-19 02:11:29

How to use if else in Join?

I have an apartments table , inside this publisher_type column, how can I make it so that if publisher_type is 0 then Join user_table, and when publisher_type is 1 then Join organization_table?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2018-11-19
@Stalker_RED

Like so?

select  foo from apartments a
left join user_table u on u.id=a.user_id AND a.publisher_type=0
left join organization_table  o on o.id=a.organization_id AND a.publisher_type=1

P
ponaehal, 2018-11-19
@ponaehal

Heh. At one time I used the following life hack:
SELECT ..... WHERE ..... AND publisher_type= 0
UNION ALL -- it is important not UNION, but UNION ALL (google the difference)
SELECT ..... WHERE ..... AND publisher_type <> 0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question