自动递增第二列 [英] auto increment second column
本文介绍了自动递增第二列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个有 2 列的表格.ID 列自动递增.我正在尝试使用与 id
列相同的 ID 自动递增用户列,但带有用户"前缀(例如:user100,其中 ID 也是 100)基本上就像什么是在 stackoverflow 上完成.
I have a table with 2 columns. The ID column auto increments. I'm trying to auto increment the user column with the same ID as the id
column, but with a "user" prefix (example: user100, where the ID is also 100) basically just like what is done on stackoverflow.
CREATE TABLE test_table (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
user CHAR(30) NOT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM;
有没有办法在 1 个查询中做到这一点?而不是插入到数据库中,然后查询获取ID,然后将ID插入到用户列中?
Is there a way of doing this in 1 query? Instead of inserting into the DB, then querying to get the ID, and inserting the ID into the user column?
推荐答案
使用 BEFORE 触发器:
Use a BEFORE trigger:
DELIMITER $$
CREATE TRIGGER test_table_trigger
BEFORE INSERT ON test_table
FOR EACH ROW BEGIN
SET NEW.`user` = CONCAT(NEW.`user`, NEW.id);
END $$
DELIMITER ;
文档:MySQL 触发器
这篇关于自动递增第二列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文