我们如何使用ORDER BY子句创建MySQL视图?
我们可以使用MySQLORDERBY子句对结果集中的记录进行排序。。为了理解带有视图的GROUPBY子句,我们使用具有以下数据的基表'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; 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; 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)