我是 PHP/MYSQL 新手。我有兩張桌子。表員工有(id,全名),表出勤有(id,staff_id)。我打算進(jìn)行查詢以獲取出勤表中 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> ';}令人驚訝的是,這會(huì)返回出勤表中存在的所有員工。請(qǐng)幫幫我
1 回答

holdtom
TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊
id我打算進(jìn)行查詢以獲取表staff_id中沒有他們的所有員工attendance。
為此,您不需要兩個(gè)查詢加上一些 PHP 邏輯。您可以使用以下命令在單個(gè)查詢中獲得所需的結(jié)果not exists:
select s.*
from staff s
where not exists (select 1 from attendance a where a.staff_id = s.id)
- 1 回答
- 0 關(guān)注
- 186 瀏覽
添加回答
舉報(bào)
0/150
提交
取消