It is easy to get sorted output based on columns that are not part of the result set or are not in the result set. Just select the required fields and write the names of the fields according to the desired sort order. Below is an example where we have sorted a result set with "Name" and "Address" fields based on the "id" column.
mysql> Select Name, Subject From Student ORDER BY Id; +---------+-----------+ | Name | Subject | +---------+-----------+ | Gaurav | Computers | | Aarav | History | | Harshit | Commerce | | Raman | Computers | +---------+-----------+ 4 rows in set (0.00 sec)
We can also use DESC or ASC keyword as shown below
mysql> Select Name, Subject from Student ORDER BY Id DESC; +---------+-----------+ | Name | Subject | +---------+-----------+ | Raman | Computers | | Harshit | Commerce | | Aarav | History | | Gaurav | Computers | +---------+-----------+ 4 rows in set (0.00 sec)
The above is the detailed content of How to sort MySQL output based on a column that does not exist in the result set?. For more information, please follow other related articles on the PHP Chinese website!