如果 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
时间: 2024-04-18 09:32:54 浏览: 75
好的,根据你的要求,如果 `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。请确认以上操作是否正确。
相关问题
如果 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怎么写
你可以使用以下 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` 表的正确名称,并根据实际情况调整查询条件。
SELECT count( DISTINCT homework_id ) finishHomeworkCount, count( 1 ) AS homeworkTitleCount, COALESCE ( ROUND(( sum( question_actual_result )/ NULLIF( count( 1 ), 0 )) * 100, 2 ), 0.00 ) AS homeworkCurrentRate, CASE WHEN homeworkCurrentRate = 100 THEN 'A' WHEN homeworkCurrentRate >= 80 AND homeworkCurrentRate < 100 THEN 'B' WHEN homeworkCurrentRate >= 60 AND homeworkCurrentRate < 80 THEN 'C' WHEN homeworkCurrentRate < 60 THEN 'D' END FROM zhzy_homework_homework_student_question zhhsq WHERE student_id = 490 AND question_actual_result IS NOT NULL AND homework_type = 1 AND update_time BETWEEN "2023-07-15" AND "2023-07-16" GROUP BY homework_id > 1054 - Unknown column 'homeworkCurrentRate' in 'field list' 如何修改
要解决这个问题,你需要做两个步骤:
1. 将 `homeworkCurrentRate` 移动到 `HAVING` 子句中,因为它是在 `SELECT` 子句中定义的别名。
2. 将 `CASE` 语句移动到外部查询中,以便你可以在 `SELECT` 子句中引用它。
下面是修改后的查询语句:
```sql
SELECT
finishHomeworkCount,
homeworkTitleCount,
homeworkCurrentRate,
CASE
WHEN homeworkCurrentRate = 100 THEN 'A'
WHEN homeworkCurrentRate >= 80 AND homeworkCurrentRate < 100 THEN 'B'
WHEN homeworkCurrentRate >= 60 AND homeworkCurrentRate < 80 THEN 'C'
WHEN homeworkCurrentRate < 60 THEN 'D'
END AS grade
FROM (
SELECT
COUNT(DISTINCT homework_id) AS finishHomeworkCount,
COUNT(1) AS homeworkTitleCount,
COALESCE(ROUND((SUM(question_actual_result) / NULLIF(COUNT(1), 0)) * 100, 2), 0.00) AS homeworkCurrentRate
FROM
zhzy_homework_homework_student_question zhhsq
WHERE
student_id = 490
AND question_actual_result IS NOT NULL
AND homework_type = 1
AND update_time BETWEEN "2023-07-15" AND "2023-07-16"
GROUP BY
homework_id
HAVING
homeworkCurrentRate IS NOT NULL
) AS subquery;
```
这样修改后的查询将会计算出每个作业的完成数量、作业标题数量以及作业的当前进度百分比,并根据百分比给出一个等级。
阅读全文