用 if....then 更新 table
update table with if....then
我的数据库中有这个 table:
GroupStandings
+--------+----------+--------------+--------+
| TeamID | GoalsFor | GoalsAgainst | Points |
+--------+----------+--------------+--------+
| 1 | 0 | 0 | 0 |
| 2 | 0 | 0 | 0 |
| 3 | 0 | 0 | 0 |
| 4 | 0 | 0 | 0 |
+--------+----------+--------------+--------+
我想编写一个查询,根据我将 GoalsFor 和 GoalsAgainst 更新为什么来更新点。
澄清一个非常糟糕的例子:
update GroupStandings
set GoalsFor = 3, GoalsAgainst = 1,
if GoalsFor>GoalsAgainst set points = 3
else if GoalsFor=GoalsAgainst set points =1
else set points = 0
where TeamID = 1
不确定您的示例查询的工作版本是否是您想要实现的全部,但如果是这样您想要使用 CASE
:
update GroupStandings
set GoalsFor = 3
, GoalsAgainst = 1
, points = CASE WHEN GoalsFor>GoalsAgainst THEN 3
WHEN GoalsFor=GoalsAgainst THEN 1
ELSE 0
END
where TeamID = 1
更新:
基于所有值为 0
的开始,您可以将 Points
设为计算列:
ALTER TABLE YourTable DROP COLUMN Points;
GO
ALTER TABLE YourTable ADD Points AS (CASE WHEN GoalsFor>GoalsAgainst THEN 3
WHEN GoalsFor=GoalsAgainst THEN 1
ELSE 0
END);
或者您可以 运行 2 次更新:
update GroupStandings
set GoalsFor = 3
, GoalsAgainst = 1
where TeamID = 1
GO
update GroupStandings
set points = CASE WHEN GoalsFor>GoalsAgainst THEN 3
WHEN GoalsFor=GoalsAgainst THEN 1
ELSE 0
END
where TeamID = 1
或者您可以使用变量来存储您的更新值并在单个更新中使用它们:
DECLARE @GoalsFor INT = 3
,@GoalsAgainst INT = 1
update GroupStandings
set GoalsFor = @GoalsFor
, GoalsAgainst = @GoalsAgainst
, points = CASE WHEN @GoalsFor>@GoalsAgainst THEN 3
WHEN @GoalsFor=@GoalsAgainst THEN 1
ELSE 0
END
where TeamID = 1
检查此代码,希望对您有所帮助:
在此代码中,您可以在变量中设置 GoalsFor 和 GoalsAgainst 值,然后进行比较以针对每个 teamid 更新分数。
Declare @GoalsFor int
Declare @GoalsAgainst int
Set @GoalsFor = 3
Set @GoalsAgainst = 1
Update GroupStandings Set GoalsFor = @GoalsFor, GoalsAgainst = @GoalsAgainst,
Points = Case When @GoalsFor > @GoalsAgainst then 3
When @GoalsFor = @GoalsAgainst then 1
else 0 end
Where TeamId = 1
select * from GroupStandings
我的数据库中有这个 table:
GroupStandings
+--------+----------+--------------+--------+
| TeamID | GoalsFor | GoalsAgainst | Points |
+--------+----------+--------------+--------+
| 1 | 0 | 0 | 0 |
| 2 | 0 | 0 | 0 |
| 3 | 0 | 0 | 0 |
| 4 | 0 | 0 | 0 |
+--------+----------+--------------+--------+
我想编写一个查询,根据我将 GoalsFor 和 GoalsAgainst 更新为什么来更新点。 澄清一个非常糟糕的例子:
update GroupStandings
set GoalsFor = 3, GoalsAgainst = 1,
if GoalsFor>GoalsAgainst set points = 3
else if GoalsFor=GoalsAgainst set points =1
else set points = 0
where TeamID = 1
不确定您的示例查询的工作版本是否是您想要实现的全部,但如果是这样您想要使用 CASE
:
update GroupStandings
set GoalsFor = 3
, GoalsAgainst = 1
, points = CASE WHEN GoalsFor>GoalsAgainst THEN 3
WHEN GoalsFor=GoalsAgainst THEN 1
ELSE 0
END
where TeamID = 1
更新:
基于所有值为 0
的开始,您可以将 Points
设为计算列:
ALTER TABLE YourTable DROP COLUMN Points;
GO
ALTER TABLE YourTable ADD Points AS (CASE WHEN GoalsFor>GoalsAgainst THEN 3
WHEN GoalsFor=GoalsAgainst THEN 1
ELSE 0
END);
或者您可以 运行 2 次更新:
update GroupStandings
set GoalsFor = 3
, GoalsAgainst = 1
where TeamID = 1
GO
update GroupStandings
set points = CASE WHEN GoalsFor>GoalsAgainst THEN 3
WHEN GoalsFor=GoalsAgainst THEN 1
ELSE 0
END
where TeamID = 1
或者您可以使用变量来存储您的更新值并在单个更新中使用它们:
DECLARE @GoalsFor INT = 3
,@GoalsAgainst INT = 1
update GroupStandings
set GoalsFor = @GoalsFor
, GoalsAgainst = @GoalsAgainst
, points = CASE WHEN @GoalsFor>@GoalsAgainst THEN 3
WHEN @GoalsFor=@GoalsAgainst THEN 1
ELSE 0
END
where TeamID = 1
检查此代码,希望对您有所帮助:
在此代码中,您可以在变量中设置 GoalsFor 和 GoalsAgainst 值,然后进行比较以针对每个 teamid 更新分数。
Declare @GoalsFor int
Declare @GoalsAgainst int
Set @GoalsFor = 3
Set @GoalsAgainst = 1
Update GroupStandings Set GoalsFor = @GoalsFor, GoalsAgainst = @GoalsAgainst,
Points = Case When @GoalsFor > @GoalsAgainst then 3
When @GoalsFor = @GoalsAgainst then 1
else 0 end
Where TeamId = 1
select * from GroupStandings