diff options
-rw-r--r-- | mysql-test/r/select.result | 10 | ||||
-rw-r--r-- | mysql-test/t/select.test | 9 | ||||
-rw-r--r-- | sql/item.cc | 11 | ||||
-rw-r--r-- | sql/sql_lex.cc | 1 | ||||
-rw-r--r-- | sql/sql_lex.h | 2 | ||||
-rw-r--r-- | sql/sql_select.cc | 6 |
6 files changed, 38 insertions, 1 deletions
diff --git a/mysql-test/r/select.result b/mysql-test/r/select.result index 3c7a74c0c57..299e0b6bf33 100644 --- a/mysql-test/r/select.result +++ b/mysql-test/r/select.result @@ -2431,3 +2431,13 @@ AND FK_firma_id = 2; COUNT(*) 0 drop table t1; +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; diff --git a/mysql-test/t/select.test b/mysql-test/t/select.test index 6626397e9e5..2adf4f1749c 100644 --- a/mysql-test/t/select.test +++ b/mysql-test/t/select.test @@ -1983,3 +1983,12 @@ AND FK_firma_id = 2; 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; diff --git a/sql/item.cc b/sql/item.cc index 8737cc06bbd..c3845db904c 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -348,7 +348,18 @@ bool Item_field::fix_fields(THD *thd,TABLE_LIST *tables) { Field *tmp; if (!(tmp=find_field_in_tables(thd,this,tables))) + { + if (thd->lex.select_lex.is_item_list_lookup) + { + Item** res= find_item_in_list(this, thd->lex.select_lex.item_list); + if (res && *res && (*res)->type() == Item::FIELD_ITEM) + { + set_field((*((Item_field**)res))->field); + return 0; + } + } return 1; + } set_field(tmp); } else if (thd && thd->set_query_id && field->query_id != thd->query_id) diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 20aacf42be0..9fb35e3f914 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -154,6 +154,7 @@ LEX *lex_start(THD *thd, uchar *buf,uint length) lex->slave_thd_opt=0; lex->sql_command=SQLCOM_END; bzero((char *)&lex->mi,sizeof(lex->mi)); + lex->select_lex.is_item_list_lookup= 0; return lex; } diff --git a/sql/sql_lex.h b/sql/sql_lex.h index ab78555262f..d4b20c69bf2 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -121,7 +121,7 @@ typedef struct st_select_lex ignore_index, *ignore_index_ptr; List<Item_func_match> ftfunc_list; uint in_sum_expr, sort_default; - bool create_refs, braces; + bool create_refs, braces, is_item_list_lookup; st_select_lex *next; } SELECT_LEX; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 29c15741347..5292a1fc0e0 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -6845,8 +6845,14 @@ find_order_in_list(THD *thd,TABLE_LIST *tables,ORDER *order,List<Item> &fields, return 0; } order->in_field_list=0; + /* Allow lookup in select's item_list to find aliased fields */ + thd->lex.select_lex.is_item_list_lookup= 1; if ((*order->item)->fix_fields(thd,tables) || thd->fatal_error) + { + thd->lex.select_lex.is_item_list_lookup= 0; return 1; // Wrong field + } + thd->lex.select_lex.is_item_list_lookup= 0; all_fields.push_front(*order->item); // Add new field to field list order->item=(Item**) all_fields.head_ref(); return 0; |