我正在postgresql中创建一个计划表。我希望用户能够更新一行中的任何列。如果用户仅更改一个值,则希望更新该值,而所有其他值保持与数据库中相同。我尝试做一个直接的update语句,任何未定义的输入都会将列值更改为null。
这是我的解决方案
create table schedule (
id serial primary key,
instructor_id integer references users(id),
holidays boolean,
sunday_morning text,
sunday_afternoon text,
monday_morning text,
monday_afternoon text,
tuesday_morning text,
tuesday_afternoon text,
wednesday_morning text,
wednesday_afternoon text,
thursday_morning text,
thursday_afternoon text,
friday_morning text,
friday_afternoon text,
saturday_morning text,
saturday_afternoon text
);
插入声明
insert into schedule (
instructor_id,
holidays,
sunday_morning,
sunday_afternoon,
monday_morning,
monday_afternoon,
tuesday_morning,
tuesday_afternoon,
wednesday_morning,
wednesday_afternoon,
thursday_morning,
thursday_afternoon,
friday_morning,
friday_afternoon,
saturday_morning,
saturday_afternoon
) values (2, false, null, '7 - 9', null, null, '4 - 7', null, null, null, '9 - 11', null, null, null, null, null )
更新声明
update schedule
set
holidays = case when true <> NULL then true else holidays end ,
sunday_morning = case when 'asd' <> NULL then 'asd' else sunday_morning end,
sunday_afternoon = case when 'asd' <> NULL then 'asd' else sunday_afternoon end,
monday_morning = case when 'asd' <> NULL then 'asd' else monday_morning end,
monday_afternoon = case when 'asd' <> NULL then 'asd' else monday_afternoon end,
tuesday_morning = case WHEN 'asd' <> NULL then 'asd' else tuesday_morning end,
tuesday_afternoon = case when 'asd' <> NULL then 'asd' else tuesday_afternoon end,
wednesday_morning = case WHEN 'asd' <> NULL then 'asd' else wednesday_morning end,
wednesday_afternoon = case when 'asd' <> NULL then 'asd' else wednesday_afternoon end,
thursday_morning = case when 'asd' <> NULL then 'asd' else thursday_morning end,
thursday_afternoon = case when 'asd' <> NULL then 'asd' else thursday_afternoon end,
friday_morning = case when 'asd' <> NULL then 'asd' else friday_morning end,
friday_afternoon = case when 'asd' <> NULL then 'asd' else friday_afternoon end,
saturday_morning = case when 'asd' <> NULL then 'asd' else saturday_morning end,
saturday_afternoon = case when 'asd' <> NULL then 'asd' else saturday_afternoon end
where instructor_id = 1
select * from schedule
asd只是伪数据。我不知道为什么会破裂。 我尝试了较小的规模,并且效果很好,但是现在却抛出了错误。