Loading mysql-test/r/order_by.result +27 −0 Original line number Diff line number Diff line Loading @@ -854,6 +854,33 @@ b a 20 1 10 2 DROP TABLE t1; CREATE TABLE t1 (a INT); INSERT INTO t1 VALUES (1),(2); SELECT a + 1 AS num FROM t1 ORDER BY 30 - num; num 3 2 SELECT CONCAT('test', a) AS str FROM t1 ORDER BY UPPER(str); str test1 test2 SELECT a + 1 AS num FROM t1 GROUP BY 30 - num; num 3 2 SELECT a + 1 AS num FROM t1 HAVING 30 - num; num 2 3 SELECT a + 1 AS num, num + 1 FROM t1; ERROR 42S22: Unknown column 'num' in 'field list' SELECT a + 1 AS num, (select num + 2 FROM t1 LIMIT 1) FROM t1; num (select num + 2 FROM t1 LIMIT 1) 2 4 3 5 SELECT a.a + 1 AS num FROM t1 a JOIN t1 b ON num = b.a; ERROR 42S22: Unknown column 'num' in 'on clause' DROP TABLE t1; CREATE TABLE t1 (a int, b int, PRIMARY KEY (a)); INSERT INTO t1 VALUES (1,1), (2,2), (3,3); explain SELECT t1.b as a, t2.b as c FROM Loading mysql-test/t/order_by.test +16 −0 Original line number Diff line number Diff line Loading @@ -577,6 +577,22 @@ INSERT INTO t1 VALUES (1,30), (2,20), (1,10), (2,30), (1,20), (2,10); DROP TABLE t1; # # Bug #22457: Column alias in ORDER BY works, but not if in an expression # CREATE TABLE t1 (a INT); INSERT INTO t1 VALUES (1),(2); SELECT a + 1 AS num FROM t1 ORDER BY 30 - num; SELECT CONCAT('test', a) AS str FROM t1 ORDER BY UPPER(str); SELECT a + 1 AS num FROM t1 GROUP BY 30 - num; SELECT a + 1 AS num FROM t1 HAVING 30 - num; --error 1054 SELECT a + 1 AS num, num + 1 FROM t1; SELECT a + 1 AS num, (select num + 2 FROM t1 LIMIT 1) FROM t1; --error 1054 SELECT a.a + 1 AS num FROM t1 a JOIN t1 b ON num = b.a; DROP TABLE t1; # End of 4.1 tests # Loading sql/item.cc +30 −3 Original line number Diff line number Diff line Loading @@ -3707,11 +3707,38 @@ bool Item_field::fix_fields(THD *thd, Item **reference) Item** res= find_item_in_list(this, thd->lex->current_select->item_list, &counter, REPORT_EXCEPT_NOT_FOUND, ¬_used); if (res != (Item **)not_found_item && (*res)->type() == Item::FIELD_ITEM) if (res != (Item **)not_found_item) { if ((*res)->type() == Item::FIELD_ITEM) { /* It's an Item_field referencing another Item_field in the select list. use the field from the Item_field in the select list and leave the Item_field instance in place. */ set_field((*((Item_field**)res))->field); return 0; } else { /* It's not an Item_field in the select list so we must make a new Item_ref to point to the Item in the select list and replace the Item_field created by the parser with the new Item_ref. */ Item_ref *rf= new Item_ref(db_name,table_name,field_name); if (!rf) return 1; thd->change_item_tree(ref, rf); /* Because Item_ref never substitutes itself with other items in Item_ref::fix_fields(), we can safely use the original pointer to it even after fix_fields() */ return rf->fix_fields(thd, tables, ref) || rf->check_cols(1); } } } if ((ret= fix_outer_field(thd, &from_field, reference)) < 0) goto error; Loading Loading
mysql-test/r/order_by.result +27 −0 Original line number Diff line number Diff line Loading @@ -854,6 +854,33 @@ b a 20 1 10 2 DROP TABLE t1; CREATE TABLE t1 (a INT); INSERT INTO t1 VALUES (1),(2); SELECT a + 1 AS num FROM t1 ORDER BY 30 - num; num 3 2 SELECT CONCAT('test', a) AS str FROM t1 ORDER BY UPPER(str); str test1 test2 SELECT a + 1 AS num FROM t1 GROUP BY 30 - num; num 3 2 SELECT a + 1 AS num FROM t1 HAVING 30 - num; num 2 3 SELECT a + 1 AS num, num + 1 FROM t1; ERROR 42S22: Unknown column 'num' in 'field list' SELECT a + 1 AS num, (select num + 2 FROM t1 LIMIT 1) FROM t1; num (select num + 2 FROM t1 LIMIT 1) 2 4 3 5 SELECT a.a + 1 AS num FROM t1 a JOIN t1 b ON num = b.a; ERROR 42S22: Unknown column 'num' in 'on clause' DROP TABLE t1; CREATE TABLE t1 (a int, b int, PRIMARY KEY (a)); INSERT INTO t1 VALUES (1,1), (2,2), (3,3); explain SELECT t1.b as a, t2.b as c FROM Loading
mysql-test/t/order_by.test +16 −0 Original line number Diff line number Diff line Loading @@ -577,6 +577,22 @@ INSERT INTO t1 VALUES (1,30), (2,20), (1,10), (2,30), (1,20), (2,10); DROP TABLE t1; # # Bug #22457: Column alias in ORDER BY works, but not if in an expression # CREATE TABLE t1 (a INT); INSERT INTO t1 VALUES (1),(2); SELECT a + 1 AS num FROM t1 ORDER BY 30 - num; SELECT CONCAT('test', a) AS str FROM t1 ORDER BY UPPER(str); SELECT a + 1 AS num FROM t1 GROUP BY 30 - num; SELECT a + 1 AS num FROM t1 HAVING 30 - num; --error 1054 SELECT a + 1 AS num, num + 1 FROM t1; SELECT a + 1 AS num, (select num + 2 FROM t1 LIMIT 1) FROM t1; --error 1054 SELECT a.a + 1 AS num FROM t1 a JOIN t1 b ON num = b.a; DROP TABLE t1; # End of 4.1 tests # Loading
sql/item.cc +30 −3 Original line number Diff line number Diff line Loading @@ -3707,11 +3707,38 @@ bool Item_field::fix_fields(THD *thd, Item **reference) Item** res= find_item_in_list(this, thd->lex->current_select->item_list, &counter, REPORT_EXCEPT_NOT_FOUND, ¬_used); if (res != (Item **)not_found_item && (*res)->type() == Item::FIELD_ITEM) if (res != (Item **)not_found_item) { if ((*res)->type() == Item::FIELD_ITEM) { /* It's an Item_field referencing another Item_field in the select list. use the field from the Item_field in the select list and leave the Item_field instance in place. */ set_field((*((Item_field**)res))->field); return 0; } else { /* It's not an Item_field in the select list so we must make a new Item_ref to point to the Item in the select list and replace the Item_field created by the parser with the new Item_ref. */ Item_ref *rf= new Item_ref(db_name,table_name,field_name); if (!rf) return 1; thd->change_item_tree(ref, rf); /* Because Item_ref never substitutes itself with other items in Item_ref::fix_fields(), we can safely use the original pointer to it even after fix_fields() */ return rf->fix_fields(thd, tables, ref) || rf->check_cols(1); } } } if ((ret= fix_outer_field(thd, &from_field, reference)) < 0) goto error; Loading