我需要从结果另一个表更新表。 我有一些表
Table people
|id|level|
1 0
2 0
3 0
和桌面游戏
|id | idman | win_or_lose | game |
1 1 win 1
2 2 lose 1
3 1 win 2
4 3 lose 2
5 2 win 3
6 3 lose 3
我需要创建一个更改表人员级别的查询。 我尝试使用此查询。但这是错的。请帮我修复此查询。
我的查询。
UPDATE people p,
(select count(id) as count
from games where and idman = p.id and win_or_lose = 'win') g
SET p.level = g.count
答案 0 :(得分:0)
你可以试试这个(我相信idman是人民Id栏的FK)
UPDATE
people
JOIN (SELECT idman FROM games WHERE win_or_lose="win" GROUP BY game)a
ON people.id=a.idman
SET level=level+1
;
如果相同的人赢得了几个不同的游戏。等级计数器将继续增加。
答案 1 :(得分:0)
UPDATE people p,
(select count(id) as count
from games where idman = p.id and win_or_lose = 'win') g
SET p.level = g.count
试试这个你已添加并在where子句中没有会导致问题的任何条件,否则你是正确的。
答案 2 :(得分:0)
我会这样做:(小提琴: http://sqlfiddle.com/#!2/1c29e/1/0)
update people p join (select idman, count(*) as max_game
from games
where win_or_lose = 'win'
group by idman) x on p.id = x.idman
set p.level = x.max_game