在Mysql的Range Partition中使用时间戳

时间:2012-09-05 04:21:49

标签: mysql

我试图通过mysql表中的时间戳进行分区。但它返回了一个错误。如果我尝试NOW()也会返回错误。如何解决此错误并使用timestamp数据类型进行分区?

  CREATE TABLE tblemployeepunch (
  fld_id int(11) NOT NULL AUTO_INCREMENT,
  fld_date Varchar(15) DEFAULT NULL,
  fld_rawpunchdate varchar(25) DEFAULT NULL,
  fld_rawpunchtime varchar(25) DEFAULT NULL,
  fld_cardno varchar(50) DEFAULT NULL,
  fld_reasoncard varchar(20) DEFAULT NULL,
  fld_mode varchar(20) DEFAULT NULL,
  fld_punchdatetime varchar(50) DEFAULT NULL,
  fld_crtdate  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  UNIQUE KEY fld_id (fld_id,fld_crtdate),
  KEY in_timesheet (fld_cardno,fld_punchdatetime,fld_mode,fld_rawpunchtime),
  KEY in_emppunch (fld_cardno,fld_rawpunchdate,fld_punchdatetime)
  )
  PARTITION BY RANGE (fld_crtdate)
  (PARTITION p_Sep12 VALUES LESS THAN (TO_TIMESTAMP('2012-10-01 00:00:00')),
  PARTITION p_Oct12 VALUES LESS THAN (TO_TIMESTAMP('2012-11-01 00:00:00')),
  PARTITION p_Nov12 VALUES LESS THAN (TO_TIMESTAMP('2012-12-01 00:00:00')),
  PARTITION p_Max VALUES LESS THAN MAXVALUE );

1 个答案:

答案 0 :(得分:1)

这是解决此问题的正确查询。使用UNIX_TIMESTAMP

      CREATE TABLE tblemployeepunch (
      fld_id int(11) NOT NULL AUTO_INCREMENT,
      fld_date Varchar(15) DEFAULT NULL,
      fld_rawpunchdate varchar(25) DEFAULT NULL,
      fld_rawpunchtime varchar(25) DEFAULT NULL,
      fld_cardno varchar(50) DEFAULT NULL,
      fld_reasoncard varchar(20) DEFAULT NULL,
      fld_mode varchar(20) DEFAULT NULL,
      fld_punchdatetime varchar(50) DEFAULT NULL,
      fld_crtdate  TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
      UNIQUE KEY fld_id (fld_id,fld_crtdate),
      KEY in_timesheet (fld_cardno,fld_punchdatetime,fld_mode,fld_rawpunchtime),
      KEY in_emppunch (fld_cardno,fld_rawpunchdate,fld_punchdatetime)
    )
     PARTITION BY RANGE (UNIX_TIMESTAMP(fld_crtdate))
     (PARTITION p_Sep12 VALUES LESS THAN (UNIX_TIMESTAMP('2012-10-01 00:00:00')),
     PARTITION p_Oct12 VALUES LESS THAN (UNIX_TIMESTAMP('2012-11-01 00:00:00')),
     PARTITION p_Nov12 VALUES LESS THAN (UNIX_TIMESTAMP('2012-12-01 00:00:00')),
     PARTITION p_Max VALUES LESS THAN MAXVALUE );