根据join和case插入sqlite数据

Inserting sqlite data based on join and case

我有 2 tables:weekly_scoresteam 在 sqlite 数据库中(SQLite 版本 3.32 .2)

我想 return weekly_scores table 中每支球队的最大进球差值并分类 根据上述标准,团队 table 中的技能

我的代码基于 and this website

insert into team(skill)
values(
case 
    select max(weekly_scores.goal_differential) as max_count
    from weekly_scores
    left join team
    on weekly_scores.team_id = team.id
    where weekly_scores.goal_differential is not NULL
    group by team.id;
when max_count < 2 then team.skill = low
when max_count >= 2 then team.skill = high
end);

我收到以下错误

Result: near "select": syntax error  
At line 8:  
insert into team(skill)  
values(  
case   
    select

任何帮助都会很棒!

你可以试试这样插入select

insert into team(id, skill)
select t.id
, case when t.max_count < 2 then 'low' else 'high' end 
from (
    select team.id, max(weekly_scores.goal_differential) as max_count
    from weekly_scores
    left join team
    on weekly_scores.team_id = team.id
    where weekly_scores.goal_differential is not NULL
    group by team.id
) t 

您需要更新 table team 而不是插入新行。
使用相关子查询:

update team 
set skill = coalesce(
  (
    select case when max(w.goal_differential) < 2 then 'low' else 'high' end 
    from weekly_scores w
    where w.team_id = team.id and w.goal_differential is not NULL 
  ), 
  skill
)