Home > Database > Mysql Tutorial > body text

How do we create a MySQL view with a list of columns?

PHPz
Release: 2023-09-01 18:13:02
forward
965 people have browsed it

我们如何创建带有列列表的 MySQL 视图?

As we know that while creating a view, providing the list of columns is optional. But if we are providing the name of the columns while creating the view then the number of names in the list of columns must be the same as the number of columns retrieved by the SELECT statement.

Example

The following example will illustrate by creating the views with column list −

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)
Copy after login

In the above code, we have given two columns and after running the query to get the data from the view, it will display only the column names that we gave while creating the view.

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)
Copy after login

The above is the detailed content of How do we create a MySQL view with a list of columns?. 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