gpt4 book ai didi

mysql - 设置此/下一个自动增量值的列值

转载 作者:行者123 更新时间:2023-11-29 07:26:55 25 4
gpt4 key购买 nike

有没有什么办法可以将下面的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 |
+----+------+

是的,我知道另一种方式是

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

最佳答案

选项或方法可以通过 Virtual (Computed) Columns .

示例:

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)

关于mysql - 设置此/下一个自动增量值的列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34229802/

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