以下脚本成功地允许我搜索日期并在“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表中,'Handler'存储为数字。 在“处理程序”表中,它显示相关数字,但将其与FullName匹配。
如何将此脚本链接到“处理程序表”并在此列中显示FullName而不是存储在JOBS表中的数字?
谢谢!
答案 0 :(得分:0)
您可以进行另一次加入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 = ?