For possible needs, you can order a subquery when you have UNION:
You generate a phone book for all teachers and students.
SELECT name, phone FROM teachers UNION SELECT name, phone FROM students
First you want to display it with all the teachers, and then with all the students. Therefore, you cannot apply global order.
One solution is to enable the key for first-order enforcement, and then sort the names:
SELECT name, phone, 1 AS orderkey FROM teachers UNION SELECT name, phone, 2 AS orderkey FROM students ORDER BY orderkey, name
I think his path is more understandable than the fake compensatory result of the subquery.
iguypouf May 08 '19 at 9:21 am-05-05-08 09:21
source share