Home  >  Article  >  Database  >  MySQL view application application view

MySQL view application application view

黄舟
黄舟Original
2017-05-07 16:39:092238browse

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:

MySQL view application application view

(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=&#39;center&#39;>序列</p>
                            </td>
                            <td width="156" height="25" bgcolor="#A2D4F4">
                                <p align=&#39;center&#39;>名字</p>
                            </td>
                            <td width="156" height="25" bgcolor="#A2D4F4">
                                <p align=&#39;center&#39;>语文成绩</p>
                            </td>
                            <td width="156" height="25" bgcolor="#A2D4F4">
                                <p align=&#39;center&#39;>数学成绩</p>
                            </td>
                            <td width="156" height="25" bgcolor="#A2D4F4">
                                <p align=&#39;center&#39;>外语成绩</p>
                            </td>
                        </tr>
    <?php
    do{
?>
<tr>
    <td width="156" height="25" bgcolor="#A2D4F4">
        <p align=&#39;center&#39;><?php echo $res[&#39;student_id&#39;];?></p>
    </td>
    <td width="156" height="25" bgcolor="#A2D4F4">
        <p align=&#39;center&#39;><?php echo $res[&#39;name&#39;];?></p>
    </td>
    <td width="156" height="25" bgcolor="#A2D4F4">
        <p align=&#39;center&#39;><?php echo $res[&#39;chinese&#39;];?></p>
    </td>
    <td width="156" height="25" bgcolor="#A2D4F4">
        <p align=&#39;center&#39;><?php echo $res[&#39;mathematics&#39;];?></p>
    </td>
    <td width="156" height="25" bgcolor="#A2D4F4">
        <p align=&#39;center&#39;><?php echo $res[&#39;english&#39;];?></p>
    </td>
</tr>
<?php
    }while($res = mysqli_fetch_array($result, MYSQLI_ASSOC));
}
?>
</table>

Run the above example as shown below:

MySQL view application application view

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!

Statement:
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