现在A表(id,userid,date,type),type有两个值表示上下班,date表示上下班时间;
有B表(id,date),date表示节假日。
现在求某一段时间内 startdate ---enddate 内某一userid缺勤的日期.

解决方案 »

  1.   

    startdate 和你的 enddate 是同一天吗
      

  2.   

    不是,这个外面已经做好了,不会出现startdate和enddate 是一天的情形
      

  3.   

    select userid,t.date from (select userid,date,count(*) c from A group by date,userid) t where t.userid=100 and t.c!=2 and date not in(select date from B);
      

  4.   

    mysql> select * from A;
    +----+--------+------------+------+
    | id | userid | date       | type |
    +----+--------+------------+------+
    |  1 |    100 | 2009-08-01 |    0 |
    |  2 |    100 | 2009-08-01 |    1 |
    |  3 |    101 | 2009-08-01 |    0 |
    |  4 |    101 | 2009-08-01 |    1 |
    |  5 |    100 | 2009-08-02 |    0 |
    |  6 |    100 | 2009-08-03 |    1 |
    |  7 |    100 | 2009-08-03 |    1 |
    |  8 |    100 | 2009-08-04 |    0 |
    +----+--------+------------+------+
    8 rows in set (0.00 sec)mysql>
    mysql> select * from B;
    +----+------------+
    | id | date       |
    +----+------------+
    |  1 | 2009-08-03 |
    |  2 | 2009-08-04 |
    +----+------------+
    2 rows in set (0.00 sec)mysql>
      

  5.   

    我对缺勤的情况就是判定他一天没来两次或者没来 对上班时间和下班时间没有验证如果你有需求可以加上 where type=0 and date="2009/08/01 08:30"等等。
      

  6.   


    SET FOREIGN_KEY_CHECKS=0;
    -- ----------------------------
    -- Table structure for tbl_holiday
    -- ----------------------------
    CREATE TABLE `tbl_holiday` (
      `intHolidayID` int(11) NOT NULL auto_increment,
      `dtDate` timestamp NULL default NULL on update CURRENT_TIMESTAMP,
      PRIMARY KEY  (`intHolidayID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=gb2312;-- ----------------------------
    -- Table structure for tbl_log
    -- ----------------------------
    CREATE TABLE `tbl_log` (
      `intLogID` int(11) NOT NULL auto_increment,
      `intUserID` int(11) default NULL,
      `dtTime` timestamp NULL default NULL on update CURRENT_TIMESTAMP,
      `intType` int(5) default NULL COMMENT '0上班,1下班',
      `strIPAddress` varchar(30) default NULL,
      `strDes` varchar(2000) default NULL COMMENT '说明',
      PRIMARY KEY  (`intLogID`),
      KEY `dtLogTime` (`dtTime`)
    ) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
    -- ----------------------------
    -- Records 
    -- ----------------------------
    INSERT INTO `tbl_holiday` VALUES ('1', '2009-08-30 14:00:56');
    INSERT INTO `tbl_log` VALUES ('1', '1', '2009-08-28 10:18:46', '0', '192.168.0.5', null);
    INSERT INTO `tbl_log` VALUES ('2', '1', '2009-08-28 10:21:38', '1', '192.168.0.5', null);
    INSERT INTO `tbl_log` VALUES ('3', '1', '2009-08-27 08:56:42', '0', '192.168.0.5', null);
    INSERT INTO `tbl_log` VALUES ('4', '1', '2009-08-27 17:57:25', '1', '192.168.0.5', null);
    INSERT INTO `tbl_log` VALUES ('5', '1', '2009-08-24 09:59:37', '0', '192.168.0.5', null);
    INSERT INTO `tbl_log` VALUES ('6', '1', '2009-08-24 18:00:13', '1', '192.168.0.5', null);如果查询【8月24号,8月30号】的userid=1的缺勤,应该有8.25,8.26,8.29
      

  7.   

    现在A表(id,userid,date,type),type有两个值表示上下班,date表示上下班时间; 
    有B表(id,date),date表示节假日。 
    现在求某一段时间内 startdate ---enddate 内某一userid缺勤的日期.-------------------------------------------
    你这个写个存储过程来实现就可以了
    对startdate ---enddate进行循环,每一天与B表及A表比较,当天日期非在B表里面有日期记录,而且又是周1~5,且在A表里面没对应记录的,则为缺勤的日期(若“缺侵”的还考虑迟到和早退的,则要结合上下班时间与公司规定的上下班时间比较),然后把符合的日期插入一个临时表,在存储过程最后把临时表的结果取出即可。
      

  8.   

    因为你startdate ---enddate 一段时间内的每一天你都不知道那一条是要上班的,所以,而你也只有节日表,没有每一天是否要上班的记录标志表,所以,必须要对每一天进行循环来判断。
      

  9.   

    简单一点的办法就是,再建一个表 
    create calendar(cdate date primary key, wtype int);  //wtype  0:工作日,5:周未,9;法定假日
    然后利用EXCEL生成日期表
    insert into calendar values 
    ('2009-08-01',5),
    ('2009-08-02',5),
    ('2009-08-03',0),
    ('2009-08-04',0),
    ('2009-08-05',0),
    ('2009-08-06',0),
    ('2009-08-07',0),
    ('2009-08-08',5),
    ('2009-08-09',5),
    ('2009-08-10',0),
    ('2009-08-11',0),
    ('2009-08-12',0),
    ('2009-08-13',0),
    ('2009-08-14',0),
    ('2009-08-15',5),
    ('2009-08-16',5),
    ('2009-08-17',0),
    ('2009-08-18',0),
    ('2009-08-19',0),
    ('2009-08-20',0),
    ('2009-08-21',0),
    ('2009-08-22',5),
    ('2009-08-23',5),
    ('2009-08-24',0),
    ('2009-08-25',0),
    ('2009-08-26',0),
    ('2009-08-27',0),
    ('2009-08-28',0),
    ('2009-08-29',5),
    ('2009-08-30',5),
    ('2009-08-31',0);然后直接用SQL语句就可以了。select *
    from calendar 
    where cdate between '2009-08-24' and '2009-08-30' 
    and wtype=0
    and cdate not in (select date(dtTime) tbl_log where intUserID=1)
      

  10.   

    一般来说是列出全部日期,这样比较简单方便。
    因为:
    1法定假日和周未冲突的时候,往往会被调整,特别是黄金周,此时仅利用week 来计划周末比较复杂。
    2日期序列你可以直接通过EXEL的拖动生成,利用EXCEL中的公式weekday 可以得到星期,然后仅更新一下法定假。然后insert 到表中即可。这个calendar 表从磁盘空间角度看是很小的。但从效率上会提高很多。
      

  11.   

    有个问题是calendar里的记录是老板生成的,老板说星期天工作(老板想不就给点加班费嘛),员工星期天就来上班了,否则考勤时拿不到全勤奖。让老板设置哪天是工作日,哪天节假日,我怕他说这烦....
      

  12.   


    这个倒不麻烦,关键是你的界面做的是否方便。
    我们是用的一个WEB面面,象日历一样显示,然后用户只需要在想更改的日期的那个格子上双击,然后跳出<工作日,周末,法定假>选项,然后选择就行了。当然你需要限制仅能修改明天以后的,不能改今天之前的。