G
G
Gluck Virtualen2020-04-15 15:26:30
MySQL
Gluck Virtualen, 2020-04-15 15:26:30

How to take join method from user variable?

The task is to change the type of the join depending on some condition.
Set a variable according to some condition:

set @type = (SELECT CASE WHEN (подзапрос) = 0 
THEN 'left' 
ELSE 'inner' 
END)


Then use a variable.
@type JOIN table ON ...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim, 2020-04-15
@gluck59

Do always left join
and write in where for inner join

select a.*, b.*
from table1 a
 left join table2 b on (a.id = b.id) 
where 1=1 /*постоянное*/
and b.id is not null /*при наличии этой строки будет inner join для left join*/

K
Konstantin Tsvetkov, 2020-04-15
@tsklab

IF (подзапрос) = 0
  SELECTLEFT JOIN
ELSE
  SELECTINNER JOIN

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question