我有两个表的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