php字母数字auto_increment插入到sql数据库

标签 php mysql auto-increment

如何使用 PHP 创建将插入到数据库中的字母数字 auto_increment?我使用的数据类型是 varchar。

例如:

SD1
SD2
SD3

最佳答案

CREATE TABLE IF NOT EXISTS `Products` (
  `prefix` varchar(2) NOT NULL DEFAULT 'SD',
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY (`prefix`, `id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 
;


INSERT INTO `Products` (`name`) VALUES
('Product #1'),
('Product #2'),
('Product #3'),
('Product #4'),
('Product #5'),
('Product #6'),
('Product #7'),
('Product #8'),
('Product #9'),
('Product #10'),
('Product #11'),
('Product #12')
;

SELECT CONCAT(`prefix`,`id`) AS 'productId',
       `name`
  FROM `Products`;

给予

+-----------+-------------+
| productId | name        |
+-----------+-------------+
| SD1       | Product #1  |
| SD2       | Product #2  |
| SD3       | Product #3  |
| SD4       | Product #4  |
| SD5       | Product #5  |
| SD6       | Product #6  |
| SD7       | Product #7  |
| SD8       | Product #8  |
| SD9       | Product #9  |
| SD10      | Product #10 |
| SD11      | Product #11 |
| SD12      | Product #12 |
+-----------+-------------+

编辑

如果你想用前导零填充数字部分,你可以这样做

CREATE TABLE IF NOT EXISTS `Products` (
  `prefix` varchar(2) NOT NULL DEFAULT 'SD',
  `id` int(10) unsigned ZEROFILL NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY (`prefix`, `id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 
;

给出

+--------------+-------------+
| productId    | name        |
+--------------+-------------+
| SD0000000001 | Product #1  |
| SD0000000002 | Product #2  |
| SD0000000003 | Product #3  |
| SD0000000004 | Product #4  |
| SD0000000005 | Product #5  |
| SD0000000006 | Product #6  |
| SD0000000007 | Product #7  |
| SD0000000008 | Product #8  |
| SD0000000009 | Product #9  |
| SD0000000010 | Product #10 |
| SD0000000011 | Product #11 |
| SD0000000012 | Product #12 |
+--------------+-------------+

编辑#2

如果您使用的是 MyISAM 或 DBD 引擎(不幸的是 innodb 不是一个选项),您可以创建一个按前缀分组的自动增量

CREATE TABLE IF NOT EXISTS `Products` (
  `prefix` varchar(2) NOT NULL DEFAULT 'SD',
  `id` int(10) unsigned ZEROFILL NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY (`prefix`, `id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 
;

其中填充了

INSERT INTO `Products` (`prefix`, `name`) VALUES
('SD', 'Product SD #1'),
('SD', 'Product SD #2'),
('SD', 'Product SD #3'),
('SD', 'Product SD #4'),
('SD', 'Product SD #5'),
('SD', 'Product SD #6'),
('TE', 'Product TE #1'),
('TE', 'Product TE #2'),
('TE', 'Product TE #3'),
('TE', 'Product TE #4'),
('TE', 'Product TE #5'),
('TE', 'Product TE #6')
;

给予

+--------------+---------------+
| productId    | name          |
+--------------+---------------+
| SD0000000001 | Product SD #1 |
| SD0000000002 | Product SD #2 |
| SD0000000003 | Product SD #3 |
| SD0000000004 | Product SD #4 |
| SD0000000005 | Product SD #5 |
| SD0000000006 | Product SD #6 |
| TE0000000001 | Product TE #1 |
| TE0000000002 | Product TE #2 |
| TE0000000003 | Product TE #3 |
| TE0000000004 | Product TE #4 |
| TE0000000005 | Product TE #5 |
| TE0000000006 | Product TE #6 |
+--------------+---------------+

关于php字母数字auto_increment插入到sql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21145426/

相关文章:

php - 我需要做什么来清理 textarea 中的数据以提供给 mysql 数据库?

php - 未设置Laravel模型工厂ID

javascript - 带有 Java 的 JS 函数的 onload 不起作用

mysql - 如果我在 MySQL 中删除一个设置了 foreignkey_checks=0 的表,FK 会发生什么?

c# - 从csv导入到asp.net中的mysql数据库中删除双引号("")

MySQL 设置自动增量 "Ad Hoc"

php - 从查询字符串php中删除加号

PHP MySQL 插入 "{$clientname}"作为字符串而不是 PHP 变量

php - 根据相应行使用递增值进行更新

field - 在 Firebird 数据库中创建自动增量字段的最简单方法