简体   繁体   English

php mysql如何加入两个表

[英]php mysql how to join two table

I have these codes: 我有以下代码:

<?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>";
}
?>      

It contains search, edit, delete and view functions...now my question is...I wanted to join the two tables in the database by the column studentnumber... my table students contains the column Lastname, Firstname, Middleinitial, Course, Year, Section, Studentnumber, Violation, Punishment, Violationdate, Punishmentstartdate, CSlength, ID, CSDone, CSLeft...now my another table named students2 contains the following rows ID, Studentnumber, Violation, Punishment, Violationdate, Punishmentstartdate, CSlength, CSDone, CSLeft...I want to display the information from my both tables...for example I want to view all the records from database with a studentnumber of 20101000...do I have to inner join the tables? 它包含搜索,编辑,删除和查看功能...现在我的问题是...我想按列studentnumber来连接数据库中的两个表...我的表students包含列Lastname,Firstname,Middleinitial,Course ,年份,部分,学生编号,违规,处罚,违规日期,Punnishmentstartdate,CSlength,ID,CSDone,CSLeft ...现在我的另一个名为students2的表包含以下行ID,Studentnumber,违规,处罚,Violationdate,Punishmentstartdate,CSlength,CSDone ,CSLeft ...我想显示两个表中的信息...例如,我想查看学生人数为20101000的数据库中的所有记录...我是否必须对表进行内部联接? I'm just a newbie in php... Thank you in advance... :) 我只是php的新手...在此先谢谢... :)

This is a LEFT JOIN . 这是一个LEFT JOIN It will return all of the records from students1 , as well as any records from students2 where the record has the same studentnumber as a record in students1 : 这将返回所有的从记录students1 ,以及来自任何记录students2其中记录有相同的studentnumber作为一个记录students1

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

An INNER JOIN returns only records that produce a match, so you will only get records where there is an identical studentnumber in both students1 and students2 . 一个INNER JOIN只返回产生匹配的记录,所以你将只能得到记录,其中有相同的studentnumber两个students1students2 Based on your comments, I believe this is the style you are looking for: 根据您的评论,我相信这是您想要的样式:

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

If you want to get your head around using JOINs, I'd recommend trying both of these statements to observe the results. 如果您想使用JOIN来解决问题,建议您尝试使用这两个语句来观察结果。 Then try a few other approaches, perhaps using this excellent tutorial on the Coding Horror Blog . 然后尝试其他一些方法,也许使用Coding Horror Blog上的出色教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM