gpt4 book ai didi

php - 使用不同表的 ID 删除数据库中的多个值

转载 作者:行者123 更新时间:2023-11-29 00:27:27 26 4
gpt4 key购买 nike

我想删除数据库中主题的全部内容,删除它的id。

if(isset($_GET['subject_id'])){
$subject_id = $_GET['subject_id'];}

$deleteexams = mysql_query("SELECT * FROM exam_exams where exam_subject_id = '$subject_id'")or die(mysql_query());
while($ids = mysql_fetch_array($deleteexams)){
$selected = $ids['exam_id'];
}
$deletequestions = mysql_query("SELECT * FROM exam_questions where question_exam_id = '$selected'")or die(mysql_query());
while($ids2 = mysql_fetch_array($deletequestions)){
$selectedids = $ids2['question_id'];
}
$deleteanswers = mysql_query("SELECT * FROM exam_answers where answer_question_set_id = '$selectedids'")or die(mysql_query());
while($ids3 = mysql_fetch_array($deleteanswers)){
$selectedidsans = $ids3['answer_id'];
}

/// I want to delete all of the answers inside the questions of the exams in the subject
mysql_query("DELETE * FROM exam_answers where answer_id = '$selectedidsans'")or die(mysql_error());
/////i want to delete all the questions
mysql_query("DELETE * FROM exam_questions where answer_id = '$selectedids'")or die(mysql_error());
////this will work because this has the subject ids
mysql_query("DELETE * FROM exam_exams where exam_subject_id = '$subject_id'")or die(mysql_error());
/////and this too
mysql_query("DELETE * FROM exam_subjects where subject_id = '$subject_id'")or die(mysql_error());

我的问题是如何删除答案和问题,因为它的字段上没有 subject_id。

最佳答案

从中删除 * 不是正确的语法。

您确实需要像评论中提到的那样使用 IN,因为这样您就可以使用考试 ID 选择表中的所有 answer_id,字段名称可能会根据您的情况而改变,但逻辑是

   DELETE
FROM exam_answers
WHERE answer_id IN
(SELECT answer_id
FROM questions
WHERE question_id IN
(SELECT exam_id
FROM exams
WHERE subject_id=<yoursubjectid>))

这不是一个有效的方法,实际上你最好在每个表中存储主题 ID,即使它是多余的

关于php - 使用不同表的 ID 删除数据库中的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18307821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com