DROP TABLE IF EXISTS `times`;
CREATE TABLE `times` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `date1` timestamp NULL DEFAULT NULL,
  `isPass` int(11) DEFAULT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=gb2312;#
# Dumping data for table times
#
LOCK TABLES `times` WRITE;
/*!40000 ALTER TABLE `times` DISABLE KEYS */;INSERT INTO `times` VALUES (1,'2010-05-17 11:20:28',0);
INSERT INTO `times` VALUES (2,'2010-05-13 14:34:04',0);
INSERT INTO `times` VALUES (3,'2010-05-13 14:34:31',0);
检查一下date1字段时间是否是今天发生的,如果是isPass字段更新为1,如果不是则默认为0
得到结果为
id    isPass
1       1
2       0
3       0

解决方案 »

  1.   

    UPDATE times SET ispass=1 WHERE DATE_FORMAT(date1,'%Y-%m-%d')=DATE_FORMAT(CURDATE(),'%Y-%m-%d')
      

  2.   

    mysql> select * from `times`;
    +----+---------------------+--------+
    | Id | date1               | isPass |
    +----+---------------------+--------+
    |  1 | 2010-05-17 11:20:28 |      0 |
    |  2 | 2010-05-13 14:34:04 |      0 |
    |  3 | 2010-05-13 14:34:31 |      0 |
    +----+---------------------+--------+
    3 rows in set (0.00 sec)mysql> select id,date(date1)=curdate()
        -> from `times`;
    +----+-----------------------+
    | id | date(date1)=curdate() |
    +----+-----------------------+
    |  1 |                     1 |
    |  2 |                     0 |
    |  3 |                     0 |
    +----+-----------------------+
    3 rows in set (0.08 sec)
      

  3.   

    mysql> update times set ispass=1 where left(date1,10)=current_date();
    Query OK, 1 row affected (0.01 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    mysql> select * from times;
    +----+---------------------+--------+
    | Id | date1               | isPass |
    +----+---------------------+--------+
    |  1 | 2010-05-17 11:20:28 |      1 |
    |  2 | 2010-05-13 14:34:04 |      0 |
    |  3 | 2010-05-13 14:34:31 |      0 |
    +----+---------------------+--------+
    3 rows in set (0.00 sec)
      

  4.   

    2楼的能解释一下date(date1)=curdate()这句话的意思吗?
      

  5.   

    update `times` set isPass=date(date1)=curdate();
      

  6.   

    update `times` set isPass=(date(date1)=curdate());date(() 函数,取日期时间的日期部分
    curdate()) 今天的日期
    date(date1) =curdate() , 如果相等,则返回1,否则返回0
      

  7.   

    MySQL官方文档 http://dev.mysql.com/doc/refman/5.1/zh/index.html