Details
Description
Outside references in LEFT JOIN's ON expression produce "unknown column" error.
Create the test dataset (As far as I'm aware, there's nothing special about this choice of tables/columns):
create table t10 ( |
pk int primary key, |
a int |
);
|
insert into t10 values (1,1); |
|
create table t11 ( |
pk int primary key, |
b int |
);
|
create table t12 (a int); |
insert into t12 values (1),(2); |
Run the query Q1:
select
|
a,
|
(select count(*) |
from |
t10 left join t11 on t11.pk=t12.a |
where |
t10.pk=1
|
) as SUBQ |
from t12; |
ERROR 1054 (42S22): Unknown column 't12.a' in 'on clause'
|
Wrapping the outside reference in a subquery helps: Q2:
select
|
a,
|
(select count(*) |
from |
t10 left join t11 on t11.pk=(select t12.a from dual) |
where |
t10.pk=1
|
) as SUBQ |
from t12; |
+------+------+
|
| a | SUBQ |
|
+------+------+
|
| 1 | 1 |
|
| 2 | 1 |
|
+------+------+
|
I've tried Q1 on MySQL 8.0.22 or PostgreSQL and it worked in both.
Is it a bug MariaDB produces this error?
A similar query
select
a,
t10.pk=1
with JOIN instead of LEFT JOIN also returns an error message:
MariaDB [test]> select
-> a,
-> (select count(*)
-> from
-> t10 join t11 on t11.pk=t12.a
-> where
-> t10.pk=1
-> ) as SUBQ
-> from t12;
ERROR 1054 (42S22): Unknown column 't12.a' in 'on clause'
If we move the ON condition to WHERE
select
a,
the query returns the expected result set:
MariaDB [test]> select
-> a,
-> (select count(*)
-> from
-> t10 join t11
-> where
-> t11.pk=t12.a and t10.pk=1
-> ) as SUBQ
-> from t12;
+------+------+
| a | SUBQ |
+------+------+
| 1 | 0 |
| 2 | 0 |
+------+------+