我有两个表candidates
和candidate_subjects
,分别用于存储候选人详细信息和候选人分数。我想要一个查询来将候选评论更新为' FAIL'如果考生通过不到6个科目。要传递主题,主题的ca_score
和exam_score
的候选总和必须大于40。
以下是我编写的查询,但未提供预期的结果:
UPDATE candidates SET candidates.remark='FAIL' WHERE (select
count(candidate_subjects.id) AS total_pass from candidates,
candidate_subjects where candidates.id=candidate_subjects.candidate_id
and (candidate_subjects.ca_score + candidate_subjects.exam_score) >= 40) < 6
表格:
CREATE TABLE candidate_subjects (
id INT(10) PRIMARY KEY NOT NULL AUTO_INCREMENT,
candidate_id INT(11),
exam_type_id INT(10),
subject_id INT(10),
ca_score INT(11),
exam_score INT(6),
score_grade VARCHAR(10),
date_created VARCHAR(10),
date_modified TIMESTAMP
);
INSERT INTO `candidate_subjects` (`id`, `candidate_id`, `exam_type_id`,
`subject_id`, `ca_score`, `exam_score`, `score_grade`, `date_created`,
`date_modified`) VALUES
(1, 2, 1, 32, 22, 61, NULL, '2017-02-01', '2017-08-28 13:10:33'),
(2, 2, 1, 5, 21, 38, NULL, '2017-02-01', '2017-08-28 13:10:33'),
(3, 2, 1, 14, 21, 51, NULL, '2017-02-01', '2017-08-28 13:10:33'),
(4, 2, 1, 1, 19, 34, NULL, '2017-02-01', '2017-08-28 13:10:33'),
(5, 2, 1, 2, 23, 39, NULL, '2017-02-01', '2017-08-28 13:10:33'),
(6, 2, 1, 38, 20, 32, NULL, '2017-02-01', '2017-08-28 13:10:33'),
(7, 2, 1, 53, 24, 47, NULL, '2017-02-01', '2017-08-28 13:10:33'),
(8, 4, 1, 32, 19, 61, NULL, '2017-02-01', '2017-08-28 13:11:27'),
(9, 4, 1, 5, 22, 41, NULL, '2017-02-01', '2017-08-28 13:11:27'),
(10, 4, 1, 14, 20, 46, NULL, '2017-02-01', '2017-08-28 13:11:27'),
(11, 4, 1, 1, 23, 37, NULL, '2017-02-01', '2017-08-28 13:11:27'),
(12, 4, 1, 2, 21, 36, NULL, '2017-02-01', '2017-08-28 13:11:27'),
(13, 4, 1, 38, 22, 34, NULL, '2017-02-01', '2017-08-28 13:11:27'),
(14, 4, 1, 53, 24, 52, NULL, '2017-02-01', '2017-08-28 13:11:27'),
(15, 5, 1, 32, 20, 62, NULL, '2017-02-01', '2017-08-28 13:11:44'),
(16, 5, 1, 5, 22, 38, NULL, '2017-02-01', '2017-08-28 13:11:44');
CREATE TABLE candidates (
id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
exam_no VARCHAR(15),
surname VARCHAR(50),
other_names VARCHAR(100),
school_id INT(11),
registration_completed INT(11),
exam_scores_completed INT(5),
remark VARCHAR(10)
);
INSERT INTO candidates (id, exam_no, surname, other_names, school_id,
registration_completed, exam_scores_completed, remark) VALUES
(1, '1171052001', 'ABADO', 'MASENENGEN', 1052, 1, '1', ''),
(2, '1170938001', 'AGBA', 'NGUHER', 938, 1, '1', ''),
(3, '1170071001', 'ABEE', 'SESUUR', 71, 1, '1', ''),
(4, '1170938002', 'AHEN', 'REBECCA DOOSUUN', 938, 1, '1', '');
答案 0 :(得分:0)
你可以试试像:
UPDATE candidates SET candidates.remark='FAIL' WHERE candidate_id IN (SELECT candidate_id FROM candidate_subject WHERE candidatescorecondition>40 HAVING COUNT(*) > 6);
请先在开发环境中运行!
答案 1 :(得分:0)
要根据您的条件更新您的候选人,并且candidate_subjects
中存在他们的记录,您可以使用以下查询。
UPDATE candidates c
JOIN (select
count(id) AS total_pass ,candidate_id
from candidate_subjects
where(ca_score + exam_score) >= 40
group by candidate_id
) a on c.id = a.candidate_id
SET c.remark='FAIL'
WHERE total_pass < 6
答案 2 :(得分:0)
如果考生可以在没有参加所有考试的情况下被标记为FAIL
,您可以这样做:
update candidates set remark='FAIL'
where id in (
select candidate_id from candidate_subjects
where ca_score+exam_score > 40 -- passed subject
group by candidate_id having count(*)<6
)
但是如果候选人必须拥有至少六个科目的数据才能进行评估,则需要添加一个额外的条件(如果他/她处于中间过程中,则候选人不会被标记为失败): / p>
update candidates set remark='FAIL'
where id in (
select candidate_id from candidate_subjects
where ca_score+exam_score > 40 -- passed subject
group by candidate_id having count(*)<6
)
and id in (
select candidate_id from candidate_subjects
group by candidate_id having count(*)>=6 -- the candidate has data for at leas 6 subjects
)