0

我有两个表 Master(empname,empid) 由所有员工组成,另一个表 Transaction(empid,Presentdate) 给出日期和哪个员工在场。
如何查找特定日期范围内的缺勤人员名单

我试过下面的 Sql 查询

SELECT empid FROM Master 
where empid NOT IN(select empid  from  Transaction 
where Date(Presentdate) between '2012-11-21' and '2012-12-22')

它只返回缺勤的员工ID,我还想显示员工的缺勤日期

注意(事务表只存储员工的当前日期)

如果员工缺席,则不会将整条记录插入到事务表中

4

3 回答 3

0

没有对此进行测试,但这可能有效:

SELECT m.empid,d.Presentdate

FROM Master as m,
(select distinct Presentdate from transaction
where Date(Presentdate) between '2012-11-21' and '2012-12-22') as d 

where m.empid not in (select empid  from  Transaction 
where Presentdate=d.Presentdate)
于 2012-12-26T09:36:42.133 回答
0

这应该有效,未经测试

SELECT m.empid     AS `Empid` 
     , d.dt        AS `AbsentDate`
  FROM ( SELECT DATE(t.Presentdate) AS dt
           FROM transaction t
          WHERE t.Presentdate >= '2012-11-21' 
            AND t.Presentdate < DATE_ADD( '2012-12-22' ,INTERVAL 1 DAY)
          GROUP BY DATE(t.Presentdate)
          ORDER BY DATE(t.Presentdate)
       ) d
 CROSS
  JOIN master m
  LEFT
  JOIN  transaction p
    ON p.Presentdate >= d.dt
   AND p.Presentdate <  d.dt + INTERVAL 1 DAY
   AND p.empid = m.empid
 WHERE p.empid IS NULL
 ORDER
    BY m.empid
     , d.dt
于 2012-12-26T09:51:10.900 回答
0

// 我想你的查询会喜欢这样来获取员工姓名、身份证和他/她在场的日期。

select empid,empname,date  from  Transaction LEFT JOIN Master ON empid 
where Date(Presentdate) between '2012-11-21' and '2012-12-22'

// 出于循环目的创建了一个数组。您还将拥有 emp id 和 name

$array = array('2012-01-01','2012-01-02','2012-01-03','2012-01-05'); 
$date = new DateTime('2012-01-01'); // take first as start date
$startDate = $array[0]; 
$endDate = $array[count($array)-1];
$loop = 'yes';

while($loop == 'yes') {
    $date->add(new DateInterval('P1D'));    //add one day to start date
    if(!in_array($date->format('Y-m-d'),$array))
        $absentDate = $date->format('Y-m-d');
    if($date->format('Y-m-d') == $endDate)
        $loop = 'no';
}
print_r($absentDate);
于 2012-12-26T10:06:18.193 回答