【发布时间】:2017-09-17 10:58:47
【问题描述】:
我在 mysql 上有两个表,例如表上称为 worker,另一个称为 interventions。
“worker”表有 3 个字段:
- 身份证
- 姓名
- 电子邮件
表格“interventions”也有 3 个字段:
- 身份证
- 姓名ID
- 说明
我想在两个表之间交叉数据。 这是 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>
问题:当我打印这个时,我得到两个相同的字段(干预的 ID 和工人的 ID);
问题是:我怎样才能选择我想要的表的ID?默认情况下,选择的 ID 是查询的第一个表——'SELECT * FROM worker, interventions'——在这种情况下是表工作者。
解决了我不想要的问题:
查询这个 -> $this->NameId = "{$this->NameId}";
插入这个 -> $this->ID = "{$this->ID}";
我的目标是:我想知道是否可以查询具有相同名称的字段的不同表,并指定顺序而不是由我在查询中首先定义的顺序。
【问题讨论】:
-
这里需要使用连接。用外键链接两个表
-
@Akin 他只是在循环使用连接,而不是明确使用
INNER JOIN关键字,而是在WHERE子句上这样做。那也解决不了他的问题。
标签: php mysql database class pdo