我有兩個表的mysql的IM,例如在表被稱爲工人,另一種被稱爲干預。PHP MYSQL PDO - >使用PDO類查詢 - 選擇不同的表具有相同名稱的字段
表 「工人」 有3個領域:
- ID
- 名稱
- 電子郵件
表 「干預」 有3個領域以及:
- ID
- 填充NameID
- 說明
我想這兩個表之間的交叉數據。 這裏是PDO類的代碼:
class PdoInsideClass {
public $ID, $Name , $NameId, $Description ;
public function __construct() {
$this->ID = "{$this->ID}";
$this->Name = "{$this->Name}";
$this->NameId = "{$this->NameId}";
$this->Description = "{$this->Description}";
}
}
數據庫查詢
$myquery = $database_connection->query('SELECT * FROM
worker, interventions
WHERE worker.ID=interventions.NameId
');
$myquery->setFetchMode(PDO::FETCH_CLASS, 'PdoInsideClass');
表和環路
<table>
<tr>
<th>ID of the Intervention</th>
<th>Description Of the Job</th>
<th>Id of the Worker</th>
<th>Name of the Worker</th>
</tr>
while ($result = $myquery->fetch()) {
echo "<tr>"; //OPEN TABLE
echo '<td>' . $result->ID . "</td>"; //ID of the Intervention
echo '<td>' . $result->Description . "</td>"; //Description Of the Job
echo '<td>' . $result->ID . "</td>"; //Id of the Worker
echo '<td>' . $result->Name . "</td>"; //Name of the Worker
echo "</tr>"; // CLOSE TABLE
}
?>
</table>
問題:W¯¯當我打印這個時,我得到兩個相同的字段(工人的干預和身份證的ID);
問題是:如何選擇我想要的表的ID?默認情況下,所選ID是查詢的第一個表格 - 'SELECT * FROM worker, interventions'
- 在這種情況下是表格工作者。
,我不是在尋找解決awesers: - 這個>$this->NameId = "{$this->NameId}";
insted的 - 查詢這個>$this->ID = "{$this->ID}";
我的目標是:我想知道,如果它能夠查詢diferent表與具有相同名稱的字段,並指定順序,而不是由我在查詢中首先放置的內容定義。
您需要在這裏使用連接。使用外鍵鏈接兩個表 – Akintunde007
@Akin他只是圍繞一種方式使用連接,而不是明確使用「INNER JOIN」關鍵字,而是在'WHERE'子句中進行。那也不能解決他的問題。 – Augwa