I have a complex query which results in multiple columns which I want to UNION into a single column.
The naive UNION construct makes me place the FROM for each query to combine. I would like that FROM to be the result of the complex query.
I would like to say:
SELECT row1 AS result FROM (complex query)
UNION
SELECT row2 AS result FROM (same complex query)
UNION
SELECT row3 AS result FROM (same complex query)
...
How do I do that in MySQL 5.6.10 (I realize that CTE elegantly solves my problem), and without temp tables (my DBA is paranoid).
You can use a cte
block like below:
with cte as
(
complex query comes here
)
select row1 as result from cte
union
select row2 as result from cte
...
Using a temptable would be another alternative solution.
For more cte
examples: http://www.mysqltutorial.org/mysql-cte/
EDIT: Since OP is using an older version, this is an alternative solution:
Use a #temp table like below in your main complex query:
Select ...
into #temp
from ...
...
Then, use the #temp table for union:
select row1 as result from #temp
union
select row2 as result from #temp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.