Bug #78404 subquery + "select * from...order by 2" (2 cols) fails with unknown column '2'
Submitted: 11 Sep 2015 9:11 Modified: 11 Sep 2015 9:38
Reporter: Su Dylan Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Parser Severity:S3 (Non-critical)
Version:5.6.22, 5.6.26 OS:Any
Assigned to: CPU Architecture:Any

[11 Sep 2015 9:11] Su Dylan
Description:

output:
=======
mysql> drop table if exists t1; create table t1(c1 int, c2 int);
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 values(1,2),(2,1);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select exists( select  * from t1 order by 2);
ERROR 1054 (42S22): Unknown column '2' in 'order clause'
mysql> select  * from t1 order by 2;
+------+------+
| c1   | c2   |
+------+------+
|    2 |    1 |
|    1 |    2 |
+------+------+
2 rows in set (0.00 sec)

mysql> select version();
+------------+
| version()  |
+------------+
| 5.6.22-log |
+------------+
1 row in set (0.00 sec)

Problem:
========
In "select  * from t1 order by 2", the clause "order by 1" is processed successfully.
But in the subquery "select exists( select  * from t1 order by 2)", it tells "1054 (42S22): Unknown column '2' in 'order clause'".

Since the first one succeeds, the subquery is expected to be successful.

How to repeat:
drop table if exists t1; create table t1(c1 int, c2 int);
insert into t1 values(1,2),(2,1);
select exists( select  * from t1 order by 2);
select  * from t1 order by 2;

Suggested fix:
The following SQL succeeds:

select exists( select  * from t1 order by 2)
[11 Sep 2015 9:38] MySQL Verification Team
Hello Su Dylan,

Thank you for the report and test case.

Thanks,
Umesh