我想在我的数据库的表中插入一个新行,插入的行有一个自动增量主键,我想选择这个生成的值在另一个表中使用它。
这是我的剧本:
INSERT INTO Inventaire(`date`) VALUES (DATE(NOW());
INSERT INTO LigneInterventaire(codeArt, qteInv, numInv)
VALUES (NEW.codeArt, NEW.qte, <`Here I want that value`>);
我该怎么做?
答案 0 :(得分:1)
这是LAST_INSERT_ID()
函数的用途:
INSERT INTO LigneInterventaire(codeArt, qteInv, numInv)
VALUES (NEW.codeArt, NEW.qte, LAST_INSERT_ID());
答案 1 :(得分:1)
您可能想要使用LAST_INSERT_ID()
您可以直接在MYSQL代码中使用它
前:
INSERT INTO Inventaire(`date`) VALUES (DATE(NOW());
SET @the_id = LAST_INSERT_ID();
INSERT INTO LigneInterventaire(codeArt, qteInv, numInv)
VALUES (NEW.codeArt, NEW.qte, @the_id);