2014-04-01 169 views
0

我有这些代码: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_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>"; 
} 
?>  

它包含了搜索,编辑,删除和查看功能......现在我的问题是...我想加入的两个表数据库由列studentnumber ... 我的表学生包含列姓,名,中初级,课程,年,部分,学号,违规,处罚,Violationdate,惩罚开始日期,CSlength,ID,CSDone,CSLeft ...现在我的另一个名为students2的表包含以下行ID,学号,违规,处罚,Violationdate,惩罚开始日期,CSlength,CSDone,CSLeft ...我想要显示我的两个表中的信息...例如,我想要查看所有记录从数据库的ds从20101000的学生数量...我有内部加入表吗? 我只是一个新手在PHP ... 在此先感谢... :)

+0

请在你的问题和期望列中添加表结构。 –

+0

可能重复[MySQL连接where子句](http://stackoverflow.com/questions/1219909/mysql-join-with-where-clause) –

+0

Lastname(varchar),Firstname(varchar),Middleinitial(varchar), (日期),CSlength(int),ID(int,primary)键,自动增量),CSDone(int),CSLeft(int)... 我想获取所有列在这两个表中...鉴于另一个表没有所有的信息,因为第一个表有它.. 。@AbhikChakraborty ... – Xthiahn29

回答

0

这是一个LEFT JOIN。它将从students2返回所有的记录从students1,以及任何记录,其中记录有相同studentnumberstudents1记录:

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

只有INNER JOIN收益产生相匹配的记录,所以,你会只有在students1students2中有相同的studentnumber的记录。根据您的意见,我相信这是你要找的风格:

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

如果你想获得你的头周围使用JOIN的,我建议你尝试这两种语句来观察结果。然后尝试一些其他方法,可能使用this excellent tutorial on the Coding Horror Blog