SQL 更新 WHERE 条件来自另一个列的代码 table
SQL code for updating a column where the WHERE condition is from another table
在 SQL Server 2012 中,我有两个 table:WINE_TYPE
和 WINE
。
WINE_TYPE
是父table,wtCode
是主键(WINE
中的外键)。
我正在尝试执行将葡萄酒价格 (winePrice
) 更新 50 美分或 0.50 的代码。问题是...我的 WHERE
条件来自父 table(我只需要由领导 Amanda McPhee (wtLeadFirst='Amanda', wtLeadLast='McPhee')
酿造的葡萄酒增加 50 美分)。
内连接在这里似乎不起作用。帮助?
Update
+ Exists
使用Exists
运算符检查wtLeadFirst='Amanda'
和wtLeadLast='McPhee'
在父table
中是否存在
update W
Set Wineprice = x + Wineprice
from Wine W
where exists (select 1
from wine_type WT
where W.wtCode =WT.code
and Wt.wtLeadFirst='Amanda'
and Wt.wtLeadLast='McPhee' )
您可以使用 UPDATE ... FROM
语法将 table 更新为 JOIN
条件:
UPDATE WINE
SET WINE.WinePrice = WINE.WinePrice + 0.5
FROM
WINE
INNER JOIN
WINE_TYPE ON WINE.wtCode = WINE_TYPE.wtCode
WHERE
WINE_TYPE.wtLeadFirst='Amanda' AND WINE_TYPE.wtLeadLast='McPhee'
在 SQL Server 2012 中,我有两个 table:WINE_TYPE
和 WINE
。
WINE_TYPE
是父table,wtCode
是主键(WINE
中的外键)。
我正在尝试执行将葡萄酒价格 (winePrice
) 更新 50 美分或 0.50 的代码。问题是...我的 WHERE
条件来自父 table(我只需要由领导 Amanda McPhee (wtLeadFirst='Amanda', wtLeadLast='McPhee')
酿造的葡萄酒增加 50 美分)。
内连接在这里似乎不起作用。帮助?
Update
+ Exists
使用Exists
运算符检查wtLeadFirst='Amanda'
和wtLeadLast='McPhee'
在父table
update W
Set Wineprice = x + Wineprice
from Wine W
where exists (select 1
from wine_type WT
where W.wtCode =WT.code
and Wt.wtLeadFirst='Amanda'
and Wt.wtLeadLast='McPhee' )
您可以使用 UPDATE ... FROM
语法将 table 更新为 JOIN
条件:
UPDATE WINE
SET WINE.WinePrice = WINE.WinePrice + 0.5
FROM
WINE
INNER JOIN
WINE_TYPE ON WINE.wtCode = WINE_TYPE.wtCode
WHERE
WINE_TYPE.wtLeadFirst='Amanda' AND WINE_TYPE.wtLeadLast='McPhee'