diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result index 26a802af47842ff1a6cad55e958f244945ed6337..5d462ec2961e8456625c0e5af513f11abe08563a 100644 --- a/mysql-test/r/subselect.result +++ b/mysql-test/r/subselect.result @@ -1324,7 +1324,7 @@ create table t2 (s1 int); insert into t1 values (1); insert into t2 values (1); update t1 set s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A); -ERROR 42S02: Unknown table 'x' in field list +ERROR 42S22: Unknown column 'x.s1' in 'field list' DROP TABLE t1, t2; CREATE TABLE t1 (s1 CHAR(5) COLLATE latin1_german1_ci, s2 CHAR(5) COLLATE latin1_swedish_ci); @@ -1477,3 +1477,12 @@ phone code 89356874041 NULL 95895001874 NULL drop table t1, t2; +create table t1 (s1 int); +create table t2 (s1 int); +select * from t1 where (select count(*) from t2 where t1.s2) = 1; +ERROR 42S22: Unknown column 't1.s2' in 'where clause' +select * from t1 where (select count(*) from t2 group by t1.s2) = 1; +ERROR 42S22: Unknown column 't1.s2' in 'group statement' +select count(*) from t2 group by t1.s2; +ERROR 42S02: Unknown table 't1' in group statement +drop table t1, t2; diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test index b601838c3782384d9b9c58cccb1e4c265c6db1d3..a452bee79ea4c7b988fea4c8078425825162af15 100644 --- a/mysql-test/t/subselect.test +++ b/mysql-test/t/subselect.test @@ -693,7 +693,7 @@ INSERT INTO t1 VALUES (1); UPDATE t1 SET i=i+(SELECT MAX(i) FROM (SELECT 1) t) WHERE i=(SELECT MAX(i)); -- error 1111 UPDATE t1 SET i=i+1 WHERE i=(SELECT MAX(i)); --- error 1109 +-- error 1054 UPDATE t1 SET t.i=i+(SELECT MAX(i) FROM (SELECT 1) t); drop table t1; @@ -857,7 +857,7 @@ create table t1 (s1 int); create table t2 (s1 int); insert into t1 values (1); insert into t2 values (1); --- error 1109 +-- error 1054 update t1 set s1 = s1 + 1 where 1 = (select x.s1 as A from t2 WHERE t2.s1 > t1.s1 order by A); DROP TABLE t1, t2; @@ -986,3 +986,17 @@ CREATE TABLE t2 (code char(5) NOT NULL default '',UNIQUE KEY code (code)) TYPE=M INSERT INTO t2 VALUES ('1'),('1226'),('1245'),('1862'),('18623'),('1874'),('1967'),('6'); select c.number as phone,(select p.code from t2 p where c.number like concat(p.code, '%') order by length(p.code) desc limit 1) as code from t1 c; drop table t1, t2; + +# +# unresolved field error +# +create table t1 (s1 int); +create table t2 (s1 int); +-- error 1054 +select * from t1 where (select count(*) from t2 where t1.s2) = 1; +-- error 1054 +select * from t1 where (select count(*) from t2 group by t1.s2) = 1; +-- error 1109 +select count(*) from t2 group by t1.s2; +drop table t1, t2; + diff --git a/sql/item.cc b/sql/item.cc index 7c7cad7c290759779b5b7083bbcec09fe472c246..9caea3cee4cc4e203fcf11ac2835d02cbbeba910 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -796,6 +796,7 @@ bool Item_field::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref) if (!field) // If field is not checked { TABLE_LIST *where= 0; + bool upward_lookup= 0; Field *tmp= (Field *)not_found_field; if ((tmp= find_field_in_tables(thd, this, tables, &where, 0)) == not_found_field) @@ -825,6 +826,7 @@ bool Item_field::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref) sl; sl= (prev_unit= sl->master_unit())->outer_select()) { + upward_lookup= 1; table_list= (last= sl)->get_table_list(); if (sl->resolve_mode == SELECT_LEX::INSERT_MODE && table_list) { @@ -869,8 +871,14 @@ bool Item_field::fix_fields(THD *thd, TABLE_LIST *tables, Item **ref) return 1; else if (tmp == not_found_field && refer == (Item **)not_found_item) { - // call to return error code - find_field_in_tables(thd, this, tables, &where, 1); + if (upward_lookup) + // We can't say exactly what absend table or field + my_printf_error(ER_BAD_FIELD_ERROR, ER(ER_BAD_FIELD_ERROR), MYF(0), + full_name(), thd->where); + else + // Call to report error + find_field_in_tables(thd, this, tables, &where, 1); + return -1; } else if (refer != (Item **)not_found_item) @@ -1381,6 +1389,7 @@ bool Item_ref::fix_fields(THD *thd,TABLE_LIST *tables, Item **reference) if (!ref) { TABLE_LIST *where= 0, *table_list; + bool upward_lookup= 0; SELECT_LEX_UNIT *prev_unit= thd->lex.current_select->master_unit(); SELECT_LEX *sl= prev_unit->outer_select(); /* @@ -1399,6 +1408,7 @@ bool Item_ref::fix_fields(THD *thd,TABLE_LIST *tables, Item **reference) REPORT_ALL_ERRORS))) == (Item **)not_found_item) { + upward_lookup= 1; Field *tmp= (Field*) not_found_field; /* We can't find table field in table list of current select, @@ -1457,11 +1467,16 @@ bool Item_ref::fix_fields(THD *thd,TABLE_LIST *tables, Item **reference) return -1; else if (ref == (Item **)not_found_item && tmp == not_found_field) { - // Call to report error - find_item_in_list(this, - *(thd->lex.current_select->get_item_list()), - &counter, - REPORT_ALL_ERRORS); + if (upward_lookup) + // We can't say exactly what absend (table or field) + my_printf_error(ER_BAD_FIELD_ERROR, ER(ER_BAD_FIELD_ERROR), MYF(0), + full_name(), thd->where); + else + // Call to report error + find_item_in_list(this, + *(thd->lex.current_select->get_item_list()), + &counter, + REPORT_ALL_ERRORS); ref= 0; return 1; }