我在MySQL数据库中有一个表schools
,它有number_of_pupils
,pupil_capacity
和pupil_numbers_or_capacity
列(所有整数字段)。
我想根据以下规则设置pupil_numbers_or_capacity
字段:
我可以在一次更新查询中执行此操作吗?
答案 0 :(得分:1)
试试这个:
UPDATE schools t
SET t.pupil_numbers_or_capacity = CASE WHEN number_of_pupils > 0
THEN number_of_pupils
ELSE pupil_capacity END
WHERE t.number_of_pupils > 0
OR t.pupil_capacity > 0