我需要从另一个表的结果更新表。 我有几张 table
Table people
|id|level|
1 0
2 0
3 0
和 table 上游戏
|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
我需要创建查询以更改表 people 中的级别。 我尝试使用此查询。但这是错误的。请帮我解决这个问题。
我的查询。
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
最佳答案
我会这样做:(Fiddle: 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
关于mysql - 修复对 mysql 的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25094307/