Bug #74251 Syntax error when using DUAL dummy table in UNION subselect with ORDER BY

Submitted: 7 Oct 2014 14:54 Modified: 23 Jul 2024 14:08
Reporter: Lukas Eder Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:5.1/5.5/5.6 OS:Any
Assigned to: CPU Architecture:Any

[7 Oct 2014 14:54] Lukas Eder
Description:
When using the DUAL dummy table in UNION subqueries (for interoperability reasons), a syntax error is reported

How to repeat:
This query works:

    select 'A' as `x`
    union 
    select 'B'
    union 
    select 'C'
    order by `x` asc;

So does this one:

    select 'A' as `x` from dual
    union 
    select 'B'        from dual
    union 
    select 'C'
    order by `x` asc;

But this one doesn't:

    select 'A' as `x` from dual
    union 
    select 'B'        from dual
    union 
    select 'C'        from dual
    order by `x` asc;

A workaround would be to wrap the last subselect in parentheses:

    select 'A' as `x` from dual
    union 
    select 'B'        from dual
    union (
      select 'C'        from dual
    )
    order by `x` asc;
[7 Oct 2014 14:55] Lukas Eder
Another workaround:

    select 'A' as `x` from dual
    union 
    select 'B'        from dual
    union 
    select 'C'        from (select 1 from dual) x
    order by 1 asc;
[7 Oct 2014 15:40] MySQL Verification Team
Thank you for the bug report. Already fixed on 5.6 source:

C:\dbs>c:\dbs\5.7\bin\mysql -uroot --port=3570 --prompt="mysql 5.7 > "
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.6-m16 Source distribution

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql 5.7 > select 'A' as `x` from dual
    ->     union
    ->     select 'B'        from dual
    ->     union
    ->     select 'C'        from dual
    ->     order by `x` asc;
+---+
| x |
+---+
| A |
| B |
| C |
+---+
3 rows in set (0.00 sec)

mysql 5.7 > EXIT
Bye

C:\dbs>56

C:\dbs>c:\dbs\5.6\bin\mysql -uroot --port=3560 --debug-info --prompt="mysql 5.6 > "
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.6.22-debug Source distribution

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql 5.6 > select 'A' as `x` from dual
    ->     union
    ->     select 'B'        from dual
    ->     union
    ->     select 'C'        from dual
    ->     order by `x` asc;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order by `x` asc' at line 6
mysql 5.6 >
[7 Oct 2014 15:41] MySQL Verification Team
Sorry I meant already fixed on 5.7 source.
[7 Oct 2014 15:55] Lukas Eder
Good news, thanks for the feedback!
[23 Jul 2024 9:02] Roy Lyseng
Posted by developer:
 
Fixed with parser refactoring in version 8.0
[23 Jul 2024 14:08] Jon Stephens
Documented fix as follows in the MySQL 8.0.0 changelog, together with the fix for BUG#50510:

    This also fixes an issue with UNION subselects using FROM DUAL;
    for example, the following query raised an error:

  SELECT 'A' AS `x` FROM DUAL
    UNION
  SELECT 'B' FROM DUAL
    UNION
  SELECT 'C' FROM DUAL
    ORDER BY `x` ASC;

Closed.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.056 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.