如何以特定方式从MySQL表中获取结果集中的记录?
为了以某种特定的方式从MySQL表中获取记录的升序或降序,我们需要将ORDERBY子句与ASC或DESC关键字一起使用。如果我们不使用任何上述关键字,则MySQL默认情况下以升序返回记录。ORDERBY子句基于将使用ORDERBY子句的特定字段(升序或降序)返回结果集。假设我们要对下表的行进行排序-
mysql> Select * from Student; +--------+--------+--------+ | Name | RollNo | Grade | +--------+--------+--------+ | Gaurav | 100 | B.tech | | Aarav | 150 | M.SC | | Aryan | 165 | M.tech | +--------+--------+--------+ 3 rows in set (0.00 sec)
以下查询按“名称”的升序对表进行排序。
mysql> Select * from student order by name; +--------+--------+--------+ | Name | RollNo | Grade | +--------+--------+--------+ | Aarav | 150 | M.SC | | Aryan | 165 | M.tech | | Gaurav | 100 | B.tech | +--------+--------+--------+ 3 rows in set (0.00 sec)
下面的查询按“降序排列”对表进行排序。
mysql> Select * from student order by Grade DESC; +--------+--------+--------+ | Name | RollNo | Grade | +--------+--------+--------+ | Aryan | 165 | M.tech | | Aarav | 150 | M.SC | | Gaurav | 100 | B.tech | +--------+--------+--------+ 3 rows in set (0.00 sec)