PHP MYSQL 从两个表中获取查询

PHP MYSQL QUERY FETCH FROM TWO TABLES

拜托,我是 PHP/MYSQL 的新手。 我有两个 table。 Table 员工有 (id, fullname),Table 出勤有 (id,staff_id)。 我打算查询以获取出勤 table 中没有 ID 为 staff_id 的所有员工。以下是我的 PDO 代码:

$att = $con->prepare('SELECT member_id FROM attendance');
$att->execute();
while ($att_fetch = $att->fetch()) {
$absent = $con->prepare('SELECT * FROM members WHERE id != "'.$att_fetch['member_id'].'" ');
$absent->execute();
$absent_fetch = $absent->fetch();
echo '
  <tr>
   <td class="name" data-id="'.$absent_fetch['id'].'">'.ucwords($absent_fetch['fullname']).'</td>
  </tr> 
';
}

令人惊讶的是,此次returns全体员工出席table。 请帮帮我

I intend to make a query to fetch all staff who do not have their id as staff_id in attendance table.

您不需要两个查询加上一些 PHP 逻辑。您可以使用 not exists:

在单个查询中获得所需的结果
select s.*
from staff s
where not exists (select 1 from attendance a where a.staff_id = s.id)