找到两个表之间的区别

时间:2018-05-02 08:06:49

标签: php

我有两个表,其中一个表显示整个学生,另一个表显示来到课堂的学生列表。所以,我想比较两个表格,以获得没有上课的学生名单。

<?php  
 function fetch_data()  
 {  
      $output = '';  
      $conn = mysqli_connect("localhost", "root", "", "students");  
      $sql = "SELECT * FROM worker ORDER BY ID";  
      $result = mysqli_query($conn, $sql);  
      while($row = mysqli_fetch_array($result))  
      {       
      $output .= '<tr>  
                          <td>'.$row["ID"].'</td>  
                          <td>'.$row["Name"].'</td>  
                          <td>'.$row["Course"].'</td>  
                          <td>'.$row["mentor"].'</td>  
                     </tr>  
                          ';  
      }  
      return $output;  
 }  

 function fetch_data1()  
 {  
      $output1 = '';  
      $conn = mysqli_connect("localhost", "root", "", "students");  
      $sql = "select * from worker1 except select * from worker";  
      $results = mysqli_query($conn, $sql);  
      while($row = mysqli_fetch_array($sql))  
      {       
      $output1 .= '<tr>  
                          <td>'.$row["ID"].'</td>  
                          <td>'.$row["Name"].'</td>  
                          <td>'.$row["Course"].'</td>  
                          <td>'.$row["mentor"].'</td>  
                     </tr>  
                          ';  
      }  
      return $output1;  
 }  
 if(isset($_POST["generate_pdf"]))  
 {  
      require_once('tcpdf.php');  
      $obj_pdf = new TCPDF('P', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);  
      $obj_pdf->SetCreator(PDF_CREATOR);  
      $obj_pdf->SetTitle("Generate HTML Table Data To PDF From MySQL Database Using TCPDF In PHP");  
      $obj_pdf->SetHeaderData('', '', PDF_HEADER_TITLE, PDF_HEADER_STRING);  
      $obj_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));  
      $obj_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));  
      $obj_pdf->SetDefaultMonospacedFont('helvetica');  
      $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER);  
      $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '10', PDF_MARGIN_RIGHT);  
      $obj_pdf->setPrintHeader(false);  
      $obj_pdf->setPrintFooter(false);  
      $obj_pdf->SetAutoPageBreak(TRUE, 10);  
      $obj_pdf->SetFont('helvetica', '', 11);  
      $obj_pdf->AddPage();  
      $content = '';  
      $content .= '  
      <h4 align="center">Student Attendance Report</h4><br /> 
      <table border="1" cellspacing="0" cellpadding="3">  
           <tr>  
                <th width="25%">ID</th>  
                <th width="30%">Name</th>  
                <th width="25%">Course</th>  
                <th width="20%">Mentor</th>  
           </tr>  
      ';  
      $content .= fetch_data();  
      $content .= '</table>';  
      $obj_pdf->writeHTML($content);  
      $obj_pdf->Output('file.pdf', 'I');  
 }  
 ?>  
 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Student Attendance Report</title>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />            
      </head>  
      <body background="pic1.jpg">  
           <br />
           <div class="container">  
                <h4 align="center"> Student Attendance Report</h4><br />  
                <div class="table-responsive">  
                    <div class="col-md-12" align="right">
                    <h4 align="center"><strong>Student Attend</strong></h4>
                     <form method="post">  
                          <input type="submit" name="generate_pdf" class="btn btn-success" value="Generate PDF" />  
                     </form>  
                     </div>
                     <br/>
                     <br/>
                     <table class="table table-bordered">  
                          <tr>  
                               <th width="20%">ID</th>  
                               <th width="30%">Name</th>  
                               <th width="30%">Course</th>  
                               <th width="20%">Mentor</th>  
                          </tr>  
                     <?php  
                     echo fetch_data();  
                     ?>  
                     </table>  


        <br/>
        <h4 align="center"><strong>Student Absence</strong></h4>
        <table class="table table-bordered">  
                <tr>  
                     <th width="20%">ID</th>  
                     <th width="30%">Name</th>  
                     <th width="30%">Course</th>  
                     <th width="20%">Mentor</th>  
                     </tr>  
                  <?php  
                   echo fetch_data1();  
                   ?>  
        </table>
                </div>  
           </div>  
      </body>  
</html>  

这些代码是我用来读取显示网页中表格数据的代码。但它只显示参加课程的学生名单,而不显示缺课的学生。

worker1 table list of overall student

worker table list of student attend the class

1 个答案:

答案 0 :(得分:0)

如果worker1表包含所有学生,worker表是参与者,那么您可以在查询中使用NOT EXISTS表示缺席的人

SELECT
ID, Name,Course, mentor
from worker1 t1
WHERE NOT EXISTS (
SELECT ID, Name,Course, mentor
FROM worker t2
WHERE t1.ID = t2.ID
)

Haven没有测试过上面的查询,但想法是一样的。