如果 homework_homework_student_question 表的字段practice_submit_answer 与 questions 的answer1相等, 更新zhzy_homework_homework_student_question的question_img_result 为1 ,question_actual_score为1 ,confidence_level为1 ,question_actual_result 为1 ,反之不相等为0 ,sql怎么写
时间: 2024-04-15 20:27:57 浏览: 71
sql学习 看似不等,其实相等.sql
你可以使用以下 SQL 查询来实现你的要求:
```sql
UPDATE zhzy_homework_homework_student_question AS q
SET q.question_img_result = CASE
WHEN q.practice_submit_answer = (
SELECT answer1
FROM questions
WHERE question_id = q.question_id
) THEN 1
ELSE 0
END,
q.question_actual_score = CASE
WHEN q.practice_submit_answer = (
SELECT answer1
FROM questions
WHERE question_id = q.question_id
) THEN 1
ELSE 0
END,
q.confidence_level = CASE
WHEN q.practice_submit_answer = (
SELECT answer1
FROM questions
WHERE question_id = q.question_id
) THEN 1
ELSE 0
END,
q.question_actual_result = CASE
WHEN q.practice_submit_answer = (
SELECT answer1
FROM questions
WHERE question_id = q.question_id
) THEN 1
ELSE 0
END;
```
以上 SQL 查询会根据条件将 `zhzy_homework_homework_student_question` 表中的相应字段进行更新。请确保替换 `questions` 和 `zhzy_homework_homework_student_question` 表的正确名称,并根据实际情况调整查询条件。
阅读全文