如何更新这种表

时间:2011-07-11 07:50:03

标签: sql sqlite

/*
Duties
*/

duty_id , duty_name 

//duty_names are duty1,duty2,duty3,duty4,duty5

/*
Members-Duties
*/

id , members_id , duty_id 

/*
Members-Duty-Scores
*/

id , members_id , duty1 , duty2 , duty3 , duty4 , duty5

我想更新表格'会员 - 值班分数'但首先我想通过查询'会员 - 职责'表来了解会员的职责。会员可以做全部或部分职责。如何更新表'会员 - 值班分数'

1 个答案:

答案 0 :(得分:1)

UPDATE Members-Duty-Scores SET
duty1 = CASE WHEN (SELECT COUNT(*) FROM Members-Duties WHERE duty_id = 1 AND Members-Duties.members_id = Members-Duty-Scores.members_id) = 1 THEN 1 ELSE 0 END,
duty2 = CASE WHEN (SELECT COUNT(*) FROM Members-Duties WHERE duty_id = 2 AND Members-Duties.members_id = Members-Duty-Scores.members_id) = 1 THEN 1 ELSE 0 END,
duty3 = CASE WHEN (SELECT COUNT(*) FROM Members-Duties WHERE duty_id = 3 AND Members-Duties.members_id = Members-Duty-Scores.members_id) = 1 THEN 1 ELSE 0 END,
duty4 = CASE WHEN (SELECT COUNT(*) FROM Members-Duties WHERE duty_id = 4 AND Members-Duties.members_id = Members-Duty-Scores.members_id) = 1 THEN 1 ELSE 0 END,
duty5 = CASE WHEN (SELECT COUNT(*) FROM Members-Duties WHERE duty_id = 5 AND Members-Duties.members_id = Members-Duty-Scores.members_id) = 1 THEN 1 ELSE 0 END