我有 sid = xxxx。
还有一个表名为“room”、列名为 sid1、sid2 和 sid3 的表。 我如何检查哪一列为空并在这些空列中的任一列中插入 sid 值。
sid1 sid2 sid3 roomno
NULL 2222 NULL 1
5432 NULL NULL 2
最佳答案
如果您只有 3 列,这还不算太糟糕。 。 .
update room
set sid1 = (case when sid1 is null then $newval else sid1 end),
sid2 = (case when sid1 is not null and sid2 is null then $newval else sid2 end),
sid3 = (case when sid1 is not null and sid2 is not null and sid3 is null then $newval else sid3 end)
where roomno = $roomno;
但是,我强烈建议您创建一个联结表,每个 sid 一行,每个房间一个。
关于mysql - 选择一行中的两个空列并在其中任意一列中插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43121588/