Home  >  Article  >  Database  >  How to get records from a MySQL table in a result set in a specific way?

How to get records from a MySQL table in a result set in a specific way?

WBOY
WBOYforward
2023-09-10 18:41:041205browse

如何以特定方式从结果集中的 MySQL 表中获取记录?

To get records from a MySQL table in a result set in a specific way (ascending or descending order), we need to use the ORDER BY clause along with the ASC or DESC keyword. If we don't use any of the above keywords, then MySQL returns the records in ascending order by default. ORDER BY clause returns a result set based on a specific field (ascending or descending order), we will use ORDER BY clause. Suppose we want to sort the rows of the following table -

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)

The following query sorts the table by "name" in ascending order.

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)

The following query sorts the table by "Grade in DESCENDING order".

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)

The above is the detailed content of How to get records from a MySQL table in a result set in a specific way?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete