我有两个表,一个是 team_stats,另一个是 team_summary。
team_summary 中有 16 个 tNames,在 team_stats 的 win_lose 列中,1 表示赢,0 表示输。
team_summary team_stats
tName W Period tName win_lose
AUT null 1 AUT 1
CAN null 2 AUT 1
DEN null 3 AUT 1
FIN null Total AUT 1
... 1 CAN 0
2 CAN 0
...
我想插入 team_summary 的 W 列。
当我查询
select tName, count(*) from team_stats
where Period='Total' and (win_lose) = 1 group by tName;
像这样返回 14 行,不包括 0 行。
tName count(*)
AUT 1
CAN 6
DEN 4
...
我想获取 16 行,包括这 14 个值和 2 个为 0(或空)的值。
我该怎么做?
最佳答案
你似乎想要一个更新
:
update team_summary ts left join
(select tName, count(*) as wins
from team_stats
where Period = 'Total' and (win_lose) = 1
group by tName
) t
on ts.tName = t.tName
set ts.summary = coalesce(t.wins, 0);
关于mysql - sql count (including null) from table where 两个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53494115/