2014-04-01 168 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