Home > Database > Mysql Tutorial > How can we create a MySQL view using ORDER BY clause?

How can we create a MySQL view using ORDER BY clause?

王林
Release: 2023-09-22 21:01:06
forward
1093 people have browsed it

我们如何使用 ORDER BY 子句创建 MySQL 视图?

We can use the MySQL ORDER BY clause to sort the records in the result set. . To understand the GROUP BY clause with a view, we create a view named "Info" using the base table "Student_info" with the following data -

mysql> Select * from Student_info;
+------+---------+------------+------------+
| id   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 105  | Gaurav  | Chandigarh | Literature |
| 125  | Raman   | Shimla     | Computers  |
| 130  | Ram     | Jhansi     | Computers  |
| 132  | Shyam   | Chandigarh | Economics  |
| 133  | Mohan   | Delhi      | Computers  |
+------+---------+------------+------------+
6 rows in set (0.00 sec)
Copy after login

Syntax

Create or Replace View view_name AS Select_statements FROM table ORDER BY expression [ASC|DESC]
Copy after login

Example

mysql> Create or Replace View Info AS select ID, Name, Address , Subject FROM Student_info ORDER BY Name ASC;
Query OK, 0 rows affected (0.11 sec)

mysql> Select * from info;
+------+---------+------------+------------+
| ID   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 105  | Gaurav  | Chandigarh | Literature |
| 133  | Mohan   | Delhi      | Computers  |
| 130  | Ram     | Jhansi     | Computers  |
| 125  | Raman   | Shimla     | Computers  |
| 132  | Shyam   | Chandigarh | Economics  |
| 101  | YashPal | Amritsar   | History    |
+------+---------+------------+------------+
6 rows in set (0.00 sec)

mysql> Create or Replace View Info AS select ID, Name, Address , Subject FROM Student_info ORDER BY Name DESC;
Query OK, 0 rows affected (0.10 sec)

mysql> Select * from info;
+------+---------+------------+------------+
| ID   | Name    | Address    | Subject    |
+------+---------+------------+------------+
| 101  | YashPal | Amritsar   | History    |
| 132  | Shyam   | Chandigarh | Economics  |
| 125  | Raman   | Shimla     | Computers  |
| 130  | Ram     | Jhansi     | Computers  |
| 133  | Mohan   | Delhi      | Computers  |
| 105  | Gaurav  | Chandigarh | Literature |
+------+---------+------------+------------+
6 rows in set (0.00 sec)
Copy after login

The above is the detailed content of How can we create a MySQL view using ORDER BY clause?. 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