create table test(
emp_id                          int                              not null  ,
exam_date                       smalldatetime                        null  ,
exam_result                     char(2)                              null  
)字段说明:
emp_id      人员序号
exam_date   考核日期
exam_result 考核结果  (优秀='11')查询--连续两年考核为优秀的员工记录?

解决方案 »

  1.   


    /*Table structure for table `test2` */DROP TABLE IF EXISTS `test2`;CREATE TABLE `test2` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `emp_id` int(11) NOT NULL,
      `exam_date` datetime DEFAULT NULL,
      `exam_result` char(2) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;/*Data for the table `test2` */insert  into `test2`(`id`,`emp_id`,`exam_date`,`exam_result`) values (1,1,'2007-08-02 00:00:00','11'),
    (2,6,'2007-11-02 00:00:00','10'),
    (3,7,'2007-11-02 00:00:00','11'),
    (4,1,'2006-08-02 00:00:00','11'),
    (5,6,'2006-11-02 00:00:00','7'),
    (6,7,'2006-11-02 00:00:00','11');查询语句 select a.emp_id from test2 a,test2 b 
    where a.emp_id = b.emp_id 
    and a.id != b.id 
    and year(a.exam_date) = year(b.exam_date)-1
    and a.exam_result = '11';
    结果:
    query result(2 records)
    emp_id