[英]Joining two tables and displaying results - php/mysql
以下脚本成功地允许我搜索日期并在“ JOBS”表中显示与存储在“ orderno”表中的时间有关的所有信息。
// PUT your connection data HERE !
$DB_SERVER = 'localhost';
$DB_NAME = 'database';
$DB_USER = 'username';
$DB_PASS = '*********';
// opening a connection to the database
try
{
$db = new PDO("mysql:host=".$DB_SERVER.";dbname=".$DB_NAME.";charset=utf8", $DB_USER, $DB_PASS, array(PDO::ERRMODE_EXCEPTION, PDO::FETCH_ASSOC) );
}
catch(PDOException $e)
{
echo $e->getMessage();
}
$sql =
"SELECT o.JobNumber, o.date_col, Job_Title, Handler
FROM orderno o
INNER JOIN JOBS j on (o.JobNumber = j.JobNo)
WHERE date_col = ?";
// preparing the query
$stmt = $db->prepare($sql);
// assembling the requested date
$date = $_GET['year'] . '-' . $_GET['month'] . '-' . $_GET['day'];
// setting the parameter value
$stmt->bindParam(1, $date, PDO::PARAM_STR);
// executing the query
$stmt->execute();
// returning the resultset
$resultSet = $stmt->fetchAll();
if ( count($resultSet) == 0 ) {
echo "No records found for date ".$date;
}
else {
echo '<table border=1>';
// writing the table header
echo '<tr>';
foreach($resultSet[0] as $key => $value) {
if (!is_numeric($key)) {
echo '<TH>'.$key.'</TH>';
}
}
echo '</tr>';
// writing the rows...
foreach($resultSet as $row) {
echo '<tr>';
// each field...
for($i = 0; $i < $stmt->columnCount(); $i++) {
echo '<td>'.$row[$i].'</td>';
}
echo '</tr>';
}
echo '</table>';
}
?></center>
但是,在“ JOBS表”中,“处理程序”存储为数字。 在“处理程序”表中,它显示相关编号,但与全名匹配。
如何将此脚本链接到“处理程序表”并在此列中显示全名,而不是在JOBS表中存储的编号?
谢谢!
您可以在INNER JOIN HANDLER h ON h.row_id = j.Handler
进行另一个INNER JOIN HANDLER h ON h.row_id = j.Handler
,然后将FUllName
添加到SELECT
SELECT o.JobNumber, o.date_col, j.Job_Title, h.FullName
FROM orderno o
INNER JOIN JOBS j ON o.JobNumber = j.JobNo
INNER JOIN HANDLER h ON h.row_id = j.Handler
WHERE o.date_col = ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.