A
A
Alexander Avtomonov2014-07-08 14:21:53
PostgreSQL
Alexander Avtomonov, 2014-07-08 14:21:53

Is it possible to use CASE in LEFT JOIN?

Is it possible to execute a query like this in Postgres:

SELECT
                                 c.controltype AS controltype,
                                 c.coretype AS coretype,
                                 g.name AS group_name,
                                 core.name AS corename,
                               FROM
                                 controls AS c
                               LEFT JOIN
                                 groups AS g
                                 ON
                                   g.id = c.group_id
                               LEFT JOIN
                                 (CASE
                                   WHEN coretype = 'Программа'
                                     THEN programs
                                   WHEN coretype = 'Дисциплина'
                                     THEN disciplines
                                   WHEN coretype = 'Модуль'
                                     THEN modules
                                   END)
                                 AS core
                                 ON
                                   c.core_id = core.id
                               LEFT JOIN
                                 users AS u
                                 ON
                                   u.id = c.teacher_id
                               WHERE
                                 id = :id

That is, depending on any value of the selected row, attach the table. Postgres makes a selection after all the tables have been reduced, but maybe there is still some kind of workaround?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question