我有两张桌子。第一个是student
,由name,id,ic,sessi,course,company_code
组成。第二个表格为company
,由company_code,company_name,company_town,company_state
组成。当我搜索student
id
时,如何显示如下?
NAME:
ID:
IC:
COURSE:
COMPANY_CODE:
COMPANY_NAME:
COMPANY_TOWN:
COMPANY_STATE:
SESSI:
答案 0 :(得分:0)
这是一个非常基本的SQL代码。您需要的是加入。
我们需要将这些表与一个公共变量连接起来,我能看到的唯一常见变量是company_code。所以语法就像dis。
Select A.name as Name, A.student_id as ID,
A.IC as IC, A.course as course,
A.company_code as company_code,B.company_name as company_name,
B.company_town as company_town,B.company_state as company_state,
A.sessi as sessi
from student A , company B
where A.company_code=B.company_code and Student_id=<id goes here>
要在PHP中显示类似的结果,请在PHP页面中使用以下内容
<?php
$sql = "<write entire query here mentioned above>";
$con = mysql_connect('localhost', 'sqluser', 'sqlpass');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test_db");
$result = mysql_query($sql, $con);
while ($row = mysql_fetch_array($result)) {
echo 'Name'.' : '.$row['A.name'];
echo 'ID'.' : '.$row['A.student_id'];
echo 'IC'.' : '.$row['A.ic'];
echo 'course'.' : '.$row['A.course'];
echo 'Company_Code'.' : '.$row['A.company_code'];
echo 'Company_name'.' : '.$row['B.company_name'];
echo 'Company_town'.' : '.$row['B.company_town'];
echo 'Company_state'.' : '.$row['B.company_state'];
echo 'Sessi'.' : '.$row['A.Sessi'];
}
mysql_close($con);
?>