diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result index 5c57ea87f21..d905024672b 100644 --- a/mysql-test/r/subselect.result +++ b/mysql-test/r/subselect.result @@ -1126,3 +1126,20 @@ id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY t1 ref salary salary 5 const 1 Using where 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away drop table t1; +(SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0); +a +1 +CREATE TABLE `t1` ( +`id` mediumint(8) unsigned NOT NULL auto_increment, +`pseudo` varchar(35) NOT NULL default '', +`email` varchar(60) NOT NULL default '', +PRIMARY KEY (`id`), +UNIQUE KEY `email` (`email`), +UNIQUE KEY `pseudo` (`pseudo`), +) TYPE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC; +INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1'); +SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1); +a b +test test +test1 test1 +drop table if exists t1; diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test index 8e8a3dfe1d4..7ba75e3dbb0 100644 --- a/mysql-test/t/subselect.test +++ b/mysql-test/t/subselect.test @@ -714,3 +714,20 @@ create table t1 (id int not null auto_increment primary key, salary int, key(sal insert into t1 (salary) values (100),(1000),(10000),(10),(500),(5000),(50000); explain SELECT id FROM t1 where salary = (SELECT MAX(salary) FROM t1); drop table t1; + +# +# reduced subselect in ORDER BY & GROUP BY clauses +# +(SELECT 1 as a) UNION (SELECT 1) ORDER BY (SELECT a+0); + +CREATE TABLE `t1` ( + `id` mediumint(8) unsigned NOT NULL auto_increment, + `pseudo` varchar(35) NOT NULL default '', + `email` varchar(60) NOT NULL default '', + PRIMARY KEY (`id`), + UNIQUE KEY `email` (`email`), + UNIQUE KEY `pseudo` (`pseudo`), +) TYPE=MyISAM CHARSET=latin1 PACK_KEYS=1 ROW_FORMAT=DYNAMIC; +INSERT INTO t1 (id,pseudo,email) VALUES (1,'test','test'),(2,'test1','test1'); +SELECT pseudo as a, pseudo as b FROM t1 GROUP BY (SELECT a) ORDER BY (SELECT id*1); +drop table if exists t1; diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 85ebb9f207f..71dfec8cac2 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -7167,9 +7167,11 @@ find_order_in_list(THD *thd, Item **ref_pointer_array, } order->in_field_list=0; Item *it= *order->item; - if (it->fix_fields(thd, tables, order->item) || it->check_cols(1) || + if (it->fix_fields(thd, tables, order->item) || + //'it' ressigned because fix_field can change it + (it= *order->item), it->check_cols(1) || thd->is_fatal_error) - return 1; // Wrong field + return 1; // Wrong field uint el= all_fields.elements; all_fields.push_front(it); // Add new field to field list ref_pointer_array[el]= it;