diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index e4f2671eb34..7e7d0617b51 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -2456,3 +2456,19 @@ f1() 42 drop view v2,v1; drop function f1; +create table t1 (id numeric, warehouse_id numeric); +create view v1 as select id from t1; +create view v2 as +select t1.warehouse_id, v1.id as receipt_id +from t1, v1 where t1.id = v1.id; +insert into t1 (id, warehouse_id) values(3, 2); +insert into t1 (id, warehouse_id) values(4, 2); +insert into t1 (id, warehouse_id) values(5, 1); +select v2.receipt_id as alias1, v2.receipt_id as alias2 from v2 +order by v2.receipt_id; +alias1 alias2 +3 3 +4 4 +5 5 +drop view v2, v1; +drop table t1; diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test index 6d2afb908a4..718e1a9932b 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -2319,3 +2319,22 @@ CREATE VIEW v2 AS SELECT f1(); select * from v2; drop view v2,v1; drop function f1; + +# +# Bug#14861: aliased column names are not preserved. +# +create table t1 (id numeric, warehouse_id numeric); +create view v1 as select id from t1; +create view v2 as +select t1.warehouse_id, v1.id as receipt_id +from t1, v1 where t1.id = v1.id; + +insert into t1 (id, warehouse_id) values(3, 2); +insert into t1 (id, warehouse_id) values(4, 2); +insert into t1 (id, warehouse_id) values(5, 1); + +select v2.receipt_id as alias1, v2.receipt_id as alias2 from v2 +order by v2.receipt_id; + +drop view v2, v1; +drop table t1; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 9b5b4b90ccb..1485f88610a 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -8189,7 +8189,7 @@ Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type, uint convert_blob_length) { Item::Type orig_type= type; - Item *orig_item; + Item *orig_item= 0; if (type != Item::FIELD_ITEM && item->real_item()->type() == Item::FIELD_ITEM && @@ -8240,10 +8240,12 @@ Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type, } else result= create_tmp_field_from_field(thd, (*from_field= field->field), - item->name, table, - modify_item ? field : - NULL, - convert_blob_length); + orig_item ? orig_item->name : + item->name, + table, + modify_item ? field : + NULL, + convert_blob_length); if (orig_type == Item::REF_ITEM && orig_modify) ((Item_ref*)orig_item)->set_result_field(result); return result;