我有这些表:
table 1 : attendance
-------------------------------
ID | DATE | EMPLOYEE_ID |
-------------------------------
1 2013-09-10 1
2 2013-09-10 2
3 2013-09-10 3
-------------------------------
table 2: employee
---------------
ID | NAME |
---------------
1 Smith
2 John
3 Mark
4 Kyle
5 Susan
6 Jim
---------------
我显示员工选项的实际代码。
while($row=mysql_fetch_array($query)){
echo "<option value='$row[employee_id]'>$row[first_name] $row[last_name]</option>";
}
?>
如何显示表 1 中未注册的员工列表?
条件是如果员工已经在表 1 中注册,他们将不会出现在选项中。
我想在 <option>
中显示列表的 <select>
元素。所以它将返回:kyle、susan、jim。
请告诉我正确的查询,或者如果有更好的选择,那也很好。请给出一些解决方案并解释。非常感谢
更新/编辑:
它也基于当前日期,如果表 1 中没有最新日期,例如今天是 2013 年 9 月 15 日。它将显示所有员工。
最佳答案
您可以使用 left join
执行此操作,然后检查是否有匹配项:
select e.*
from employee e left outer join
attendance a
on e.id = a.employee_id
where a.employee_id is null;
这可能是 MySQL 中最有效的选项。
编辑:
要包含特定日期,请将条件添加到 on
子句:
select e.*
from employee e left outer join
attendance a
on e.id = a.employee_id and a.date = date('2013-09-20')
where a.employee_id is null;
关于php - MySQL 为考勤案例选择行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18739786/