diff --git a/mysql-test/r/derived.result b/mysql-test/r/derived.result index 03e00b206b2..06d2e663221 100644 --- a/mysql-test/r/derived.result +++ b/mysql-test/r/derived.result @@ -1,3 +1,7 @@ +SELECT 1 as a FROM (SELECT 1 UNION SELECT a) b; +Unknown column 'a' in 'field list' +SELECT 1 as a FROM (SELECT a UNION SELECT 1) b; +Unknown column 'a' in 'field list' drop table if exists t1,t2,t3; CREATE TABLE t1 (a int not null, b char (10) not null); insert into t1 values(1,'a'),(2,'b'),(3,'c'),(3,'c'); diff --git a/mysql-test/t/derived.test b/mysql-test/t/derived.test index 5f63cea3c11..b9c9ab8a718 100644 --- a/mysql-test/t/derived.test +++ b/mysql-test/t/derived.test @@ -1,3 +1,7 @@ +-- error 1054 +SELECT 1 as a FROM (SELECT 1 UNION SELECT a) b; +-- error 1054 +SELECT 1 as a FROM (SELECT a UNION SELECT 1) b; drop table if exists t1,t2,t3; CREATE TABLE t1 (a int not null, b char (10) not null); insert into t1 values(1,'a'),(2,'b'),(3,'c'),(3,'c'); diff --git a/sql/item.cc b/sql/item.cc index d05578c800a..65a9422d8f1 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -573,7 +573,8 @@ bool Item_field::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref) Item **refer= (Item **)not_found_item; // Prevent using outer fields in subselects, that is not supported now SELECT_LEX *cursel=(SELECT_LEX *) thd->lex.current_select; - if (cursel->linkage != DERIVED_TABLE_TYPE) + if (cursel->master_unit()->first_select()->linkage != + DERIVED_TABLE_TYPE) for (SELECT_LEX *sl=cursel->outer_select(); sl; sl= sl->outer_select()) @@ -586,7 +587,8 @@ bool Item_field::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref) REPORT_EXCEPT_NOT_FOUND)) != (Item **)not_found_item) break; - if (sl->linkage == DERIVED_TABLE_TYPE) + if (sl->master_unit()->first_select()->linkage == + DERIVED_TABLE_TYPE) break; // do not look over derived table } if (!tmp) @@ -1024,7 +1026,8 @@ bool Item_ref::fix_fields(THD *thd,TABLE_LIST *tables, Item **reference) if ((ref= find_item_in_list(this, *(thd->lex.current_select->get_item_list()), ((sl && - thd->lex.current_select->linkage != + thd->lex.current_select->master_unit()-> + first_select()->linkage != DERIVED_TABLE_TYPE) ? REPORT_EXCEPT_NOT_FOUND : REPORT_ALL_ERRORS))) == @@ -1050,7 +1053,8 @@ bool Item_ref::fix_fields(THD *thd,TABLE_LIST *tables, Item **reference) if ((tmp= find_field_in_tables(thd, this, sl->get_table_list(), 0)) != not_found_field); - if (sl->linkage == DERIVED_TABLE_TYPE) + if (sl->master_unit()->first_select()->linkage == + DERIVED_TABLE_TYPE) break; // do not look over derived table }