如何连接两个表有一个将限制在php mysql


How to join two tables having one will be limit in php mysql

我很难配置如何连接两个表,第二个表应该是限制的。

下面是我的表格样本。

员工表:

id EmpID Lastname Firstname Department
1  4     Joe      Dylan     Admin      
2  5     Black    Teddy     Admin

这是考勤表:

EmpID  Date      TimeIn  TimeOut
4      2015-1-1  07:45   17:15
4      2015-1-2  08:15   15:00
4      2015-1-3  08:05   17:00
5      2015-1-1  08:00   16:00
5      2015-1-2  09:00   18:00

我想按EmpID组合这两个表,并且每页只显示一个员工。

这是我期望结果的一个示例:

EmpID: 4  
Dept: Admin
Name: Joe, Dylan
Date    TimeIn    TimeOut
2015-1-1  07:45   17:15
2015-1-2  08:15   15:00
2015-1-3  08:05   17:00

您只需要将where condition添加为:

$id=4;
$query="select employee.* ,attendance.* from (select * from employee where EmpID='".$id."') employee inner join attendance on employee.EmpID=attendance.EmpID";

Try

SELECT * from employee 
LEFT JOIN attendance on attendance.EmpID=employee.EmpID

这将是所有员工的考勤结果;然而,如果你想每页一个员工记录,那么触发相同的查询PAgination;

SELECT * from employee 
LEFT JOIN attendance on attendance.EmpID=employee.EmpID
WHERE employee.EmpID=per_page_emp_id

根据你的评论,你也需要日期要求,那么…

SELECT * from employee 
LEFT JOIN attendance on attendance.EmpID=employee.EmpID
WHERE employee.EmpID=per_page_emp_id AND (
 attendance.date BETWEEN state 2015-1-1 AND 2015-1-1