Bug #33096 SELECT INTO + SUBQUERY WITH UNION fails
Submitted: 9 Dec 2007 11:53 Modified: 9 Dec 2007 12:28
Reporter: Philip Stoev Email Updates:
Status: Duplicate Impact on me:
None 
Category:MySQL Server: General Severity:S3 (Non-critical)
Version:6.0-BK OS:Any
Assigned to: CPU Architecture:Any

[9 Dec 2007 11:53] Philip Stoev
Description:
BUG# 23345 fixes a situation where SELECT INTO is used on the non-last query of a UNION. Such queries are made to fail with a message "Incorrect usage of UNION and INTO". However, this message is also served on valid queries where the UNION is in a subquery and INTO is in the main query.

How to repeat:
mysql> select SUM(a) FROM (select 1 AS a UNION SELECT 2 AS a) AS b;
+--------+
| SUM(a) |
+--------+
|      3 |
+--------+
1 row in set (0.00 sec)

mysql> select SUM(a) INTO @b FROM (select 1 AS a UNION SELECT 2 AS a) AS b;
ERROR 1221 (HY000): Incorrect usage of UNION and INTO

Suggested fix:
The Changeset fixing bug #23345 says:

"  INTO clause can be specified only for the last select of a UNION and it
  receives the result of the whole query. But it was wrongly allowed in
  non-last selects of a UNION which leads to a confusing query result."

This should be constrained only to INTO appearing in queries participating in the actual UNION, not to queries above or below it in the hierarchy.
[9 Dec 2007 12:28] Sveta Smirnova
Please do not submit the same bug more than once. An existing bug report already describes this very problem. Even if you feel that your issue is somewhat different, the resolution is likely
to be the same. Because of this, we hope you add your comments to the original bug instead.

Thank you for your interest in MySQL.

Duplicate of bug #32858