我有三张桌子

CREATE TABLE guest(
name varchar(100),
ranking int,
PRIMARY KEY (name)
);

CREATE TABLE room(
roomname varchar(100),
wallcolor varchar(100),
rating int,
PRIMARY KEY(roomnane)
);

CREATE TABLE reservation(
name varchar(100),
roomname varchar(100),
day varchar(100),
moveinday int,
moveoutday int,
PRIMARY KEY(roomname, day, start, finish),
FOREIGN KEY(roomname) REFERENCES room(roomname),
FOREIGN KEY(name) REFERENCES guest(name)
);

我试图把所有的“湖”房间改成“海湾”房间,把“海湾”房间改成“湖”房间,而不明确地改变相关房间。我怎么能这样做?我尝试使用更新查询,但我不确定如何完成此操作。

最佳答案

UPDATE room
SET roomname = (
    CASE
    WHEN roomname = "Bay" THEN
        "Lake"
    WHEN roomname = "Lake" THEN
        "Bay"
    ELSE
        roomname
    END
)

例子:
如果在运行此查询之前您的房间表如下所示:
mysql - 如何通过交换两个列值来更新MySQL表?-LMLPHP
然后在运行上述查询之后,您的房间表将保持其他房间不变:
mysql - 如何通过交换两个列值来更新MySQL表?-LMLPHP

09-10 02:41