gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:54:09 35 4
gpt4 key购买 nike

如何使用 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/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com