I have a query that returns me this:
+-------------+-------------+-------------+-------------+
| L_1_teacher | L_1_student | L_2_teacher | L_2_student |
+-------------+-------------+-------------+-------------+
| 333333 | 33667 | 111111 | 33668 |
| 222222 | 33667 | 111111 | 33669 |
| 222222 | 33667 | 111111 | 33670 |
+-------------+-------------+-------------+-------------+
I need to "union" the two teacher columns. The expected output is:
+-------------+
| teachers |
+-------------+
| 333333 |
| 222222 |
| 111111 |
+-------------+
Since it's a long query, I cannot afford to run it twice (actually, I have more than two levels of teachers), select teachers on every level and union them.
What I tried: I stored the results of this query into a temp table tree
and did:
SELECT L_1_teacher as "teachers" FROM tree
UNION
SELECT L_2_teacher as "teachers" FROM tree
but it threw me an error saying Error Code: 1137. Can't reopen table: 'tree'
(MySQL prohibits using the same temporary table twice in the query)
How can I accomplish this?