我有一张用于调查的表格,并且想要删除与某个特定监视相关的所有记录。

我的桌子:

_______________     _______________     ___________  ________________
|_tblSurvey___|     |_tblAnswers___|    |_tblAlt__|  |_tblQuestions_|
| surveyID    |     | answerAltID  |    | altID   |  | questID      |
| surveyName  |     | userID       |    | altText |  | questText    |
|_____________|     |______________|    |_questID_|  |_surveyID_____|


假设我要删除与SurveyID 1相关的所有记录。

我试过了:

DELETE
 *
FROM tblSurvey, tblQuestions, tblAlt, tblAnswers
WHERE tblSurvey.surveyID = 1
AND tblsurvey.surveyID = tblQuestions.surveyID
AND tblQuestions.questID = tblAlt.questID
AND tblAlt.altID = tblAnswers.answerAltID

最佳答案

两种方式:


使用ON DELETE CASCADE设置外键约束。
使用多表DELETE语句。


尝试这个:

DELETE tblSurvey, tblQuestion, tblAlt, tblAnswers
FROM tblSurvey
JOIN tblQuestion ON tblsurvey.surveyID = tblQuestion.surveyID
JOIN tblAlt ON tblQuestions.questID = tblAlt.questID
JOIN tblAnswers ON tblAlt.altID = tblAnswers.answerAltID
WHERE tblSurvey.surveyID = 1

关于mysql - 从多个表中删除所有相关记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42613223/

10-12 15:19