如何获取mariadb的当前值?

7y4bm7vi  于 2021-06-19  发布在  Mysql
关注(0)|答案(3)|浏览(326)

我想得到当前值,即身份证号码。对于我的表,id有自动递增选项,我想得到id值。在甲骨文中,是这样的:

INSERT INTO mvc_board(bName, bTitle, bContent, bHit, bGroup, bStep,
bIndent) 
VALUES('abcd', 'is title', 'is content', 0, mvc_board_seq.currval, 0, 0);

但是,我使用的是mariadb,因此我使用了以下查询:

INSERT INTO mvc_board (bName, bTitle, bContent, bHit, bGroup, bStep, bIndent)
VALUES ('pat', 'is title', 'is content', 0, SELECT MAX(bId) FROM mvc_board, 0, 0);

但是,这有语法错误。我该怎么修?

um6iljoc

um6iljoc1#

这将使用mysql会话变量。首先将max id存储在@last\u insert\u id中,然后在查询中使用它。希望对你有帮助。

SET @last_insert_id := 0;

SELECT @last_insert_id := MAX(bId) FROM mvc_board;

INSERT INTO mvc_board (bName, bTitle, bContent, bHit, bGroup, bStep, bIndent)
VALUES ('pat', 'is title', 'is content', 0, @last_insert_id, 0, 0);
nbysray5

nbysray52#

INSERT INTO mvc_board (bName, bTitle, bContent, bHit, bGroup, bStep, bIndent)
SELECT 'pat', 'is title', 'is content', 0, MAX(bId), 0, 0 FROM mvc_board
q1qsirdb

q1qsirdb3#

下一个自动增量值在信息模式中可用:

INSERT INTO mvc_board (bName, bTitle, bContent, bHit, bGroup, bStep, bIndent)
SELECT 'pat', 'is title', 'is content', 0, AUTO_INCREMENT, 0, 0 
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = database() AND TABLE_NAME = 'mvc_board';

相关问题