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

2023年 9月 22日 64.0k 0

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

我们可以使用 MySQL ORDER BY 子句对结果集中的记录进行排序。 。为了理解带有视图的 GROUP BY 子句,我们使用具有以下数据的基表“Student_info”创建一个名为“Info”的视图 -

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)

登录后复制

语法

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

登录后复制

示例

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)

登录后复制

以上就是我们如何使用 ORDER BY 子句创建 MySQL 视图?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

Oracle如何使用授予和撤销权限的语法和示例
Awesome Project: 探索 MatrixOrigin 云原生分布式数据库
下载丨66页PDF,云和恩墨技术通讯(2024年7月刊)
社区版oceanbase安装
Oracle 导出CSV工具-sqluldr2
ETL数据集成丨快速将MySQL数据迁移至Doris数据库

发布评论