简体   繁体   English

设置此/下一个auto_increment值的列值

[英]Set column value of this/next auto_increment value

Is there any way to accomplish below sql result as a one liner. 有什么办法可以将下面的sql结果作为一个衬里来完成。

create table test( id int not null primary key auto_increment, name char(10));

insert into test (name) values ('voda'+ this_value_of_id);

// so select would return
MariaDB [testdb]> select * from test;
+----+------+
| id | name |
+----+------+
|  1 | foo1 |
+----+------+

Yes, I know the other way is 是的,我知道另一种方式是

begin transaction;
insert into test (name) values ('voda');
update test set name = concat('voda', id) where id = 1;
commit;

An option or approach can be via a Virtual (Computed) Columns . 选项或方法可以通过“ 虚拟(计算)列”进行

Example: 例:

MariaDB [testdb]> DROP TABLE IF EXISTS `test`;
Query OK, 0 rows affected (0.00 sec)

MariaDB [testdb]> CREATE TABLE `test` (
    ->   `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ->   `name` CHAR(10),
    ->   `nameid` VARCHAR(20) AS (CONCAT(`name`, `id`)) VIRTUAL
    -> );
Query OK, 0 rows affected (0.00 sec)

MariaDB [testdb]> INSERT INTO `test`
    ->   (`name`)
    -> VALUES
    ->   ('foo'), ('voda');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [testdb]> SELECT
    ->   `id`,
    ->   `nameid` `name`
    -> FROM
    ->   `test`;
+----+-------+
| id | name  |
+----+-------+
|  1 | foo1  |
|  2 | voda2 |
+----+-------+
2 rows in set (0.00 sec)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM