使用另一个表中的join和conditions(where)更新表

时间:2018-01-14 01:28:59

标签: sqlite

我有两张桌子:团队玩家

团队:TeamID,NumberOfPlayers,..

玩家:玩家ID,姓名,姓氏,团队ID,......

我想根据玩家的姓名姓氏 TeamID 更新 NumberOfPlayers

这就是我的尝试:

UPDATE Teams 
SET NumberOfPlayers = 28 
FROM Teams 
JOIN Players ON Teams.TeamID = Players.TeamID
WHERE Players.Name LIKE 'Amata' AND Players.Surname LIKE 'Walton' AND Players.TeamID = 3

但是我收到了一个错误:

near "FROM": syntax error: UPDATE Teams 
SET NumberOfPlayers = 28 
FROM

我也尝试过:

UPDATE Teams 
JOIN Players ON Teams.TeamID = Players.TeamID
SET NumberOfPlayers = 28 
WHERE Players.Name LIKE 'Amata' AND Players.Surname LIKE 'Walton' AND Players.TeamID = 3

我得到的错误是:

near "JOIN": syntax error: UPDATE Teams 
JOIN

另一个镜头是:

UPDATE Teams AS T
JOIN Players P ON T.TeamID = P.TeamID
SET T.NumberOfPlayers = 28 
WHERE P.Name LIKE 'Amata' AND P.Surname LIKE 'Walton' AND P.TeamID = 3

导致我犯了这个错误:

near "AS": syntax error: UPDATE Teams AS

似乎问题出在 UPDATE Teams 之后,但这是一个有效的表。 这可能吗?我错过了什么?

1 个答案:

答案 0 :(得分:1)

您的代码有点奇怪,因为您已经知道要更新的计数和团队ID,因此我不确定该播放器的重要性。

此外,我担心它看起来好像你可能使用名字/姓氏作为键,我强烈不建议这样做。此外,如果要在字符串上进行精确匹配,则引用=。请在此处查看详细信息:Equals(=) vs. LIKE

但我相信这是您在技术上的目标。

某些关系数据库支持更新的FROM / JOIN(如SQL Server),所以不要(像Oracle)。 SQLite不支持该语法。

UPDATE Teams 
SET NumberOfPlayers = 28 
Where TeamId In
(SELECT TeamId From Players
WHERE Players.Name LIKE 'Amata' AND Players.Surname LIKE 'Walton' AND Players.TeamID = 3
)