php mysql如何连接两个表

php mysql如何连接两个表,php,mysql,Php,Mysql,我有以下代码: <?php $records = mysql_connect('localhost', 'root', '') or die(mysql_error()); mysql_select_db('records', $records); if(isset($_GET['src'])) { $sql = "SELECT * FROM students where studentnumber like '%{$_GET['src']}%'"; $cnt = mysql_num

我有以下代码:

<?php

$records = mysql_connect('localhost', 'root', '') or die(mysql_error());

mysql_select_db('records', $records);
if(isset($_GET['src']))
{

$sql = "SELECT * FROM students where studentnumber like '%{$_GET['src']}%'";
$cnt = mysql_num_rows(mysql_query($sql));
if ($cnt == 0)
{
echo "<script>alert('No Record Found');</script>";
}

$result = mysql_query($sql, $records);
echo "<table border='0' class='table table-striped table-bordered table-hover'>";
echo "<tr class='info'><td width='10%'>Name</td><td width='11%'>Course Yr-Sec</td><td width='10%'>Student Number</td><td width='10%'>Violation</td><td width='10%'>Punishment</td><td width='9%'>Violation Date</td><td width='7%'>Punishment Date</td><td width='5%'>CS Length</td><td width='4%'>CS Done</td><td width='4%'>CS Left</td><td width='17%'><center>Action</center></td></tr></tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>";
echo $row['Lastname'];
echo ", ";
echo $row['Firstname'];
echo " ";
echo $row['Middleinitial'];
echo "</td>";
echo "<td>";
echo $row['Course'];
echo " ";
echo $row['Year'];
echo "-";
echo $row['Section'];
echo "</td>";
echo "<td>";
echo $row['Studentnumber'];
echo "</td>";
echo "<td>";
echo $row['Violation'];
echo "</td>";
echo "<td>";
echo $row['Punishment'];
echo "</td>";
echo "<td>";
echo $row['Violationdate'];
echo "</td>";
echo "<td>";
echo $row['Punishmentstartdate'];
echo "</td>";
echo "<td>";
echo $row['CSlength'];
echo "</td>";
echo "<td>";
echo $row['CSDone'];
echo "</td>";
echo "<td>";
echo $row['CSLeft'];
echo "</td>";
echo "<td>";
echo "<a href='edit.php?no={$row['ID']}'><input type='button' name='edit' value='Edit' class='btn btn-success'></a>";
echo "   <a href='delete.php?no={$row['ID']}'><input type='button' name='delete' value='Delete' class='btn btn-danger'></a>";
echo "   <input type='button' name='view' value='View' class='btn btn-info'>";echo "</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
$sql = 'SELECT * FROM students';
$result = mysql_query($sql, $records);
echo "<table border='0' class='table table-striped table-bordered table-hover'>";
echo "<tr class='info'><td width='10%'>Name</td><td width='11%'>Course Yr-Sec</td><td width='10%'>Student Number</td><td width='10%'>Violation</td><td width='10%'>Punishment</td><td width='9%'>Violation Date</td><td width='7%'>Punishment Date</td><td width='5%'>CS Length</td><td width='4%'>CS Done</td><td width='4%'>CS Left</td><td width='17%'><center>Action</center></td></tr></tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>";
echo $row['Lastname'];
echo ", ";
echo $row['Firstname'];
echo " ";
echo $row['Middleinitial'];
echo "</td>";
echo "<td>";
echo $row['Course'];
echo " ";
echo $row['Year'];
echo "-";
echo $row['Section'];
echo "</td>";
echo "<td>";
echo $row['Studentnumber'];
echo "</td>";
echo "<td>";
echo $row['Violation'];
echo "</td>";
echo "<td>";
echo $row['Punishment'];
echo "</td>";
echo "<td>";
echo $row['Violationdate'];
echo "</td>";
echo "<td>";
echo $row['Punishmentstartdate'];
echo "</td>";
echo "<td>";
echo $row['CSlength'];
echo "</td>";
echo "<td>";
echo $row['CSDone'];
echo "</td>";
echo "<td>";
echo $row['CSLeft'];
echo "</td>";
echo "<td>";
echo "<a href='edit.php?no={$row['ID']}'><input type='button' name='edit' value='Edit' class='btn btn-success'></a>";
echo "   <a href='delete.php?no={$row['ID']}'><input type='button' name='delete' value='Delete' class='btn btn-danger'></a>";
echo "   <input type='button' name='view' value='View' class='btn btn-info'>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
?>      

这是一个
左连接
。它将返回来自
students1
的所有记录,以及来自
students2
的任何记录,其中记录与
students1
中的记录具有相同的
studentnumber

SELECT * FROM students1
LEFT JOIN students2
ON students1.studentnumber = students2.studentnumber
AND students1.studentnumber = 20101000
内部联接
只返回产生匹配项的记录,因此只能在
students1
students2
中都有相同的
studentnumber
的记录。根据你的评论,我相信这就是你想要的风格:

SELECT * FROM students1
INNER JOIN students2
ON students1.studentnumber = students2.studentnumber
AND students1.studentnumber = 20101000

如果您想了解如何使用联接,我建议您尝试使用这两种语句来观察结果。然后尝试其他一些方法,可能使用。

请添加您的问题的表结构和要获取的预期列。可能重复Lastname(varchar)、Firstname(varchar)、Middleinitial(varchar)、Course(varchar)、Year(int)、Section(text)、Studentnumber(varchar)、Violationdate(varchar)、惩罚(varchar)、Violationdate(date)、惩罚StartDate(date)、CSlength(int)、ID(int、主键、自动递增)、CsOne(int)、CSLeft(int)…我想获取两个表中的所有列…假设另一个表没有第一个表所拥有的所有信息…@AbhikChakraborty。。。