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

WBOY
Release: 2023-09-10 18:41:04
forward
1149 people have browsed it

如何以特定方式从结果集中的 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)
Copy after login

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

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

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!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!