[英]How can I INSERT if row doesn't exist, else UPDATE that row?
在前端,每當我按下提交問題的答案時,它都會創建 1 個具有這些列的 result_ID。
result_ID 是自動遞增的, question_ID 是與questions
表中相同 question_ID 的關系。
如果這是用戶第一次選擇答案,它會創建一個 answer_result(我在 answer_ID 中解析)和 answer_checkResult(值 1 或 0 以標識其正確或不正確),以及一個 history_ID 以分別標識每條記錄。
History_ID 是一個不同的表,它具有 quiz_ID(用於識別主題)和 user_ID
例如:History_ID 221 里面有 4 個問題,有 4 個答案,有 4 個 answer_result。
我不知道的是,如果該行不存在,我將如何創建一種情況,它將運行 INSERT INTO 情況,否則如果它已經存在(因為用戶可以在 1 個問題中多次更改答案),它會更新。 我剛剛創建了 INSERT INTO 選項,但我不知道如何在這個模型中同時使用 INSERT INTO 進行更新。
這是我創建的 history_result.model,我不知道如何創建 if-else 以同時更新和創建...
history_result.model
const HistoryResult = function (history_result) {
this.question_ID = history_result.question_ID;
this.answer_result = history_result.answer_result;
this.answer_checkResult = history_result.answer_checkResult;
this.history_ID = history_result.history_ID;
};
HistoryResult.create = async (newHistoryResult, result) => {
await db.query(
`INSERT INTO history_result SET question_ID = ?, answer_result = ?, answer_checkResult = ?, history_ID = ?`,
[
newHistoryResult.question_ID,
newHistoryResult.answer_result,
newHistoryResult.answer_checkResult,
newHistoryResult.history_ID,
],
(err, data) => {
if (err) {
result(err, null);
return;
} else {
return result(null, data);
}
}
);
};
這是我創建 history_result 控制器的方法
const HistoryResult = require("../models/history_result.model");
exports.createHistoryResult = async (req, res) => {
let { history_ID } = req.params;
let { question_ID, answer_result, answer_checkResult } = req.body;
let historyResult = new HistoryResult({
question_ID: question_ID,
answer_result: answer_result,
answer_checkResult: answer_checkResult,
history_ID: history_ID,
});
HistoryResult.create(historyResult, (err, data) => {
if (err) {
res.status(500).send({
message: err.message || "Error while creating result",
});
}
res.send(data);
});
};
無論如何我可以做到這一點嗎? 謝謝。
是的你可以。
但首先你必須將 question_ID 設為 PRIMARY KEY。 您傳遞給 db.query 的第二個參數是包含 history_result 屬性的對象
INSERT INTO history_result
SET ?
ON DUPLICATE KEY UPDATE
answer_result = VALUES(answer_result),
answer_checkResult = VALUES(answer_checkResult),
history_ID = VALUES(history_ID)
db.query(query, objectHere, (err, data) => {
if (err) {
result(err, null);
return;
} else {
return result(null, data);
}
}))
首先,請閱讀MySQL 插入或更新重復鍵更新教程,
或此官方 MySQL INSERT ... ON DUPLICATE KEY UPDATE 聲明文檔
現在回到你的問題。 據我了解, history_result
表中的question_ID
和history_ID
對是唯一的,因為每個用戶在測驗中只會給出一個問題的答案。
首先,您需要創建表的對 (question_ID, history_ID) 的唯一索引約束。
ALTER TABLE history_result
ADD CONSTRAINT uc_question_history
UNIQUE (question_ID,history_ID);
然后發出INSERT ON DUPLICATE KEY UPDATE
語句來達到效果。
INSERT INTO history_result
(
question_ID, answer_result, history_ID
)
VALUES
(14, 21, 12)
ON DUPLICATE KEY UPDATE
answer_result = 21;
如果 question_ID = 14 和 history_ID = 12 行已經存在(用戶已經回答了這個問題的場景),它會觸發更新answer_result
。 如果沒有,它將插入一個新記錄。
如果新行在 UNIQUE 索引或 PRIMARY KEY 中重復,則滿足DUPLICATE KEY
約束。 在我們的例子中,它是 (question_ID, history_ID) 的唯一索引,因此將調用 UPDATE 語句。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.