我想获取当前值,即ID号。 对于我的表,ID具有Auto_increment选项,我想获取ID值。 在Oracle中,就像这样:
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);
但是,这有语法错误。我该如何解决?
答案 0 :(得分:1)
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
答案 1 :(得分:0)
这将使用mysql会话变量。首先将最大ID存储在@last_insert_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);
答案 2 :(得分:0)
下一个auto_increment值在INFORMATION_SCHEMA中可用:
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';