我们如何使用 order by 子句创建 mysql 视图?-kb88凯时官网登录

时间:2023-09-22
阅读:
免费资源网 - https://freexyz.cn/

我们可以使用 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 视图?的详细内容,

免费资源网 - https://freexyz.cn/
返回顶部
顶部
网站地图