mysql中的zerofill的应用

整型字段有个ZEROFILL属性,在数字长度不够的数据前面填充0,以达到设定的长度

CREATE TABLE `staffs` (
  `id` int(11) unsigned zerofill NOT NULL AUTO_INCREMENT,
  `col2` varchar(20) NOT NUL,
  PRIMARY KEY (`id`),
) ENGINE=InnoDB;

 

1.这种用法,可以大量用于所谓“流水号”的生成上。比如,想要生成日期_0x的流水号。可以直接拼接

select concat(concat(date(now()), '_'), id) from t1

 2.比较常用的应该是月份或日期前补0,还是整形数字不是字符串

CREATE TABLE `t1` (
  `year` year(4) DEFAULT NULL,
  `month` int(2) unsigned zerofill DEFAULT NULL,
  `day` int(2) unsigned zerofill DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

 ss

 

 
 

你可能感兴趣的:(mysql)