如何创建带有列表的 MySQL 视图?
mysqlmysqli database
众所周知,在创建视图时,提供列表是可选的。但如果我们在创建视图时提供列的名称,则列表名称的数量必须与 SELECT 语句检索到的列数相同。
示例
以下示例将通过创建带有列表的视图进行说明 −
mysql> Select * from student_detail; +-----------+-------------+------------+ | Studentid | StudentName | address | +-----------+-------------+------------+ | 100 | Gaurav | Delhi | | 101 | Raman | Shimla | | 103 | Rahul | Jaipur | | 104 | Ram | Chandigarh | | 105 | Mohan | Chandigarh | +-----------+-------------+------------+ 5 rows in set (0.17 sec) mysql> Create view View_student_detail_columns AS SELECT Studentid, StudentName FROM Student_Detail; Query OK, 0 rows affected (0.10 sec)
在上面,我们给出了两列,在运行查询从视图中获取数据后,它将仅显示我们在创建时给出的列名。
mysql> Select * from View_Student_detail_columns; +-----------+-------------+ | Studentid | StudentName | +-----------+-------------+ | 100 | Gaurav | | 101 | Raman | | 103 | Rahul | | 104 | Ram | | 105 | Mohan | +-----------+-------------+ 5 rows in set (0.08 sec)