Home > Database > Mysql Tutorial > body text

How to sort MySQL output based on a column that does not exist in the result set?

PHPz
Release: 2023-08-22 13:21:08
forward
1049 people have browsed it

How to sort MySQL output based on a column that does not exist in the result set?

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)
Copy after login

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)
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template