根据join和case插入sqlite数据
Inserting sqlite data based on join and case
我有 2 tables:weekly_scores 和 team 在 sqlite 数据库中(SQLite 版本 3.32 .2)
- Weekly_scores 有 team_id 和 goal_differential 包含进球数那个星期球队赢了。团队有 team.id 不同的团队
- 我想根据赛季中的最大获胜进球数对 球队 table 中的球队进行分类。
- 如果 goal_differential < 2,则 team.skill = 低
- 如果goal_differential>=2,则team.skill=高
我想 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
)
我有 2 tables:weekly_scores 和 team 在 sqlite 数据库中(SQLite 版本 3.32 .2)
- Weekly_scores 有 team_id 和 goal_differential 包含进球数那个星期球队赢了。团队有 team.id 不同的团队
- 我想根据赛季中的最大获胜进球数对 球队 table 中的球队进行分类。
- 如果 goal_differential < 2,则 team.skill = 低
- 如果goal_differential>=2,则team.skill=高
我想 return weekly_scores table 中每支球队的最大进球差值并分类 根据上述标准,团队 table 中的技能。
我的代码基于
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
)