I have a table called liturgia_calendario
with the following structure:
calendario_id ff_yy ff_mm ff_dd id_liturgia pre
1 2018 06 01 198 0
2 2018 06 02 199 360
And I need to join it with another table called liturgia
to use the data of col1, col2, ...
. The structure of this table is more or less this:
id_liturgia col1 col2...
198
199
...
360
What I want is that:
-
If the column
pre
inliturgia_calendario
equals0
, I use the value in columnid_liturgia
to do the join. -
If the column
pre
inliturgia_calendario
is different from0
, I use the value of that column to do the join.
In the case of the 1st row, the join would be done using the value 198
and in the case of the 2nd row, the value 350
would be used.
I can get one or the other value from the liturgia_calendario
table. For example, this query works to alternatively get one or another column under the alias id_liturgia
:
SELECT CASE WHEN (pre = 0)
THEN id_liturgia
ELSE pre
END AS id_liturgia
FROM liturgia_calendario
WHERE f_yy=2018 and f_mm=6;
Result:
id_liturgia
198
360
But when I try it with the JOIN
it does not work for me.
If I write the query like this:
SELECT CASE WHEN (pre = 0)
THEN id_liturgia
ELSE pre
END AS id_liturgia
FROM liturgia_calendario lc
INNER JOIN liturgia l ON lc.id_liturgia=l.id_liturgia
WHERE lc.f_yy=2018 and lc.f_mm=6;
I get the error:
Column 'id_liturgia' in field list is ambiguous
If I give another alias to the column resulting from the CASE
I get the same error:
SELECT CASE WHEN (pre = 0)
THEN id_liturgia
ELSE pre
END AS id_liturgiaz
FROM liturgia_calendario lc
INNER JOIN liturgia l ON lc.id_liturgiaz=l.id_liturgia
WHERE lc.f_yy=2018 and lc.f_mm=6;
I do not know why this happens or if there is a better way to achieve what I want.