We know that the MySQL SELECT command is used to get data from a MySQL table. When you select rows, the MySQL server is free to return them in any order unless you instruct it by stating how to sort the results. However, we can order the result set by adding an ORDER BY clause, which names the column or columns to be sorted.
Select column1, column2,…,columN From table_name ORDER BY column1[column2,…];
In the following example, MySQL returns a result set sorted by the "Name" column;
mysql> Select Id, Name, Address from Student ORDER BY Subject; +------+---------+---------+ | Id | Name | Address | +------+---------+---------+ | 15 | Harshit | Delhi | | 1 | Gaurav | Delhi | | 17 | Raman | Shimla | | 2 | Aarav | Mumbai | +------+---------+---------+ 4 rows in set (0.00 sec)
The above is the detailed content of How can we get the sorted MySQL output?. For more information, please follow other related articles on the PHP Chinese website!