[英]find the difference between two table
我有两个表,其中一个表显示了全体学生,另一个表显示了上课的学生的名单。 因此,我想比较两个表格以获得缺课的学生名单。
<?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
表包含所有学生,而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
)
尚未测试上面的查询,但想法是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.