Application view of MySQL view
In the actual Web development process, there may be many fields in multiple data tables, but a certain A module may only require a few of these fields. In order to improve query speed and facilitate operation, the fields required by this module can be extracted separately and placed in a view.
Then we introduced in detail how to modify the MySQL view in the previous article "Application of MySQL View - Modify View", so today we will continue to introduce about MySQL view, application view!
Technical Points
The view created in this example involves multi-table query, which shows that multiple tables can be combined through views. As a whole, the operation on the view is equivalent to a multi-table query. But if there are the same fields in these tables, the fields with the same name must be written as follows:
表名1.字段名,表名2.字段名……
Implementation process
(1) Create a view for the database in the "Command Prompt" window. The code is as follows:
create view scoreinfo as select student_id,name, chinese,mathematics,english from student,score where student.id=score.id
After creating the view in the "Command Prompt" window, view the view as follows:
(2) Create a database connection php file, Query the content in the view scoreinfo and display the query results. The code is as follows:
<?php header("Content-Type:text/html; charset=utf-8"); $link = mysqli_connect("localhost", "root", "root"); $conn = mysqli_select_db($link, "php_cn"); $sql = "select * from scoreinfo"; $result = mysqli_query($link, $sql); $res = mysqli_fetch_array($result, MYSQLI_ASSOC); if(!$res) { echo "没有找到你要的信息"; }else{ ?> <table width="600" height="50" border="0" align="center" cellspacing="1" cellpadding="0"> <tr> <td width="156" height="25" bgcolor="#A2D4F4"> <p align='center'>序列</p> </td> <td width="156" height="25" bgcolor="#A2D4F4"> <p align='center'>名字</p> </td> <td width="156" height="25" bgcolor="#A2D4F4"> <p align='center'>语文成绩</p> </td> <td width="156" height="25" bgcolor="#A2D4F4"> <p align='center'>数学成绩</p> </td> <td width="156" height="25" bgcolor="#A2D4F4"> <p align='center'>外语成绩</p> </td> </tr> <?php do{ ?> <tr> <td width="156" height="25" bgcolor="#A2D4F4"> <p align='center'><?php echo $res['student_id'];?></p> </td> <td width="156" height="25" bgcolor="#A2D4F4"> <p align='center'><?php echo $res['name'];?></p> </td> <td width="156" height="25" bgcolor="#A2D4F4"> <p align='center'><?php echo $res['chinese'];?></p> </td> <td width="156" height="25" bgcolor="#A2D4F4"> <p align='center'><?php echo $res['mathematics'];?></p> </td> <td width="156" height="25" bgcolor="#A2D4F4"> <p align='center'><?php echo $res['english'];?></p> </td> </tr> <?php }while($res = mysqli_fetch_array($result, MYSQLI_ASSOC)); } ?> </table>
Run the above example as shown below:
We have introduced all the applications of MySQL views here. I believe everyone is familiar with MySQL. Now that you have a certain knowledge and understanding of views, we will introduce about MySQL triggers below. Please read "MySQL Triggers to Create Triggers" for details!
【Recommended related tutorials】
1.【MYSQL online free video tutorial】
2. Recommended related video courses : "老boymysql video tutorial"
The above is the detailed content of MySQL view application application view. For more information, please follow other related articles on the PHP Chinese website!