我有此代码用于更新,但如何将其更改为 DELETE 不更新。这可能吗?
update r set r.Status = 'Checked-in' from Reservation r
inner join GuestInfo g on g.GuestID = r.GuestID
where g.GuestFName = 'firstname'
and g.GuestLName = 'lastname'
and r.RoomNo = '103'
我有此代码用于更新,但如何将其更改为 DELETE 不更新。这可能吗?
update r set r.Status = 'Checked-in' from Reservation r
inner join GuestInfo g on g.GuestID = r.GuestID
where g.GuestFName = 'firstname'
and g.GuestLName = 'lastname'
and r.RoomNo = '103'
像这样的东西应该工作:
delete r from Reservation r
inner join GuestInfo g on g.GuestID = r.GuestID
where g.GuestFName = 'firstname'
and g.GuestLName = 'lastname'
and r.RoomNo = '103'
Delete from Reservation
where id in
(select r.id from Reservation r
inner join GuestInfo g on g.GuestID = r.GuestID
where g.GuestFName = 'firstname'
and g.GuestLName = 'lastname'
and r.RoomNo = '103')
绝对地!您可以简单地摆脱这些UPDATE
部分并将关键字更改为DELETE
:
delete Reservation
from Reservation r
inner join GuestInfo g on g.GuestID = r.GuestID
where g.GuestFName = 'firstname'
and g.GuestLName = 'lastname'
and r.RoomNo = '103'
事实上,这很常见。有很多时候你需要DELETE
根据条件做一个更复杂的,而且很多时候 SELECT
和UPDATE
语句可以很容易地变成一个DELETE
. 事实上,我曾经工作过的一个地方,他们的规则是这样的:
编写
SELECT
语句,然后如果它为您获取数据,请摆脱SELECT
并将其更改为DELETE
-但只有这样。