diff --git a/mysql-test/r/select.result b/mysql-test/r/select.result index c72bc332041..fd8a0a35275 100644 --- a/mysql-test/r/select.result +++ b/mysql-test/r/select.result @@ -2737,6 +2737,16 @@ id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE t1 ALL NULL NULL NULL NULL 5 1 SIMPLE t2 ref a a 23 test.t1.a 2 DROP TABLE t1, t2; +CREATE TABLE t1 (a INT, b INT); +(SELECT a, b AS c FROM t1) ORDER BY c+1; +a c +(SELECT a, b AS c FROM t1) ORDER BY b+1; +a c +SELECT a, b AS c FROM t1 ORDER BY c+1; +a c +SELECT a, b AS c FROM t1 ORDER BY b+1; +a c +drop table t1; create table t1 (a int, b int); create table t2 like t1; select t1.a from (t1 inner join t2 on t1.a=t2.a) where t2.a=1; diff --git a/mysql-test/t/select.test b/mysql-test/t/select.test index 3bfcce832c1..477b863dec0 100644 --- a/mysql-test/t/select.test +++ b/mysql-test/t/select.test @@ -2069,6 +2069,7 @@ AND FK_firma_id = 2; drop table t1; +# # # Test for Bug#8009, SELECT failed on bigint unsigned when using HEX # @@ -2181,6 +2182,16 @@ select found_rows(); DROP TABLE t1; +# +# Bug 7672 Unknown column error in order clause +# +CREATE TABLE t1 (a INT, b INT); +(SELECT a, b AS c FROM t1) ORDER BY c+1; +(SELECT a, b AS c FROM t1) ORDER BY b+1; +SELECT a, b AS c FROM t1 ORDER BY c+1; +SELECT a, b AS c FROM t1 ORDER BY b+1; +drop table t1; + # # Bug #13356 assertion failed in resolve_const_item() # diff --git a/sql/item.cc b/sql/item.cc index 5961db0bc1f..2fa77ac81c0 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -3213,6 +3213,22 @@ bool Item_field::fix_fields(THD *thd, Item **reference) TRUE)) == not_found_field) { + + /* Look up in current select's item_list to find aliased fields */ + if (thd->lex->current_select->is_item_list_lookup) + { + uint counter; + bool not_used; + Item** res= find_item_in_list(this, thd->lex->current_select->item_list, + &counter, REPORT_EXCEPT_NOT_FOUND, + ¬_used); + if (res != not_found_item && (*res)->type() == Item::FIELD_ITEM) + { + set_field((*((Item_field**)res))->field); + return 0; + } + } + /* If there are outer contexts (outer selects, but current select is not derived table or view) try to resolve this reference in the diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index b7a2b6b0624..f6d4767089b 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -1133,6 +1133,7 @@ void st_select_lex::init_query() ref_pointer_array= 0; select_n_having_items= 0; subquery_in_having= explicit_limit= 0; + is_item_list_lookup= 0; first_execution= 1; first_cond_optimization= 1; parsing_place= NO_MATTER; @@ -1165,6 +1166,7 @@ void st_select_lex::init_select() select_limit= 0; /* denotes the default limit = HA_POS_ERROR */ offset_limit= 0; /* denotes the default offset = 0 */ with_sum_func= 0; + } /* diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 3c34c7aaaea..1c90d3ed97b 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -482,6 +482,7 @@ public: List item_list; /* list of fields & expressions */ List interval_list, use_index, *use_index_ptr, ignore_index, *ignore_index_ptr; + bool is_item_list_lookup; /* Usualy it is pointer to ftfunc_list_alloc, but in union used to create fake select_lex for calling mysql_select under results of union diff --git a/sql/sql_select.cc b/sql/sql_select.cc index ce8f9078ee1..18b48c33f5b 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -11950,11 +11950,16 @@ find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables, We check order_item->fixed because Item_func_group_concat can put arguments for which fix_fields already was called. */ + thd->lex->current_select->is_item_list_lookup= 1; if (!order_item->fixed && (order_item->fix_fields(thd, order->item) || (order_item= *order->item)->check_cols(1) || thd->is_fatal_error)) + { + thd->lex->current_select->is_item_list_lookup= 0; return TRUE; /* Wrong field. */ + } + thd->lex->current_select->is_item_list_lookup= 0; uint el= all_fields.elements; all_fields.push_front(order_item); /* Add new field to field list. */