Summary
SQLiteでUpdateするさい他のテーブルの値を使って更新する
こんな感じ
upsertを使う
-- SalaryIDはSalaryテーブルのプライマリキー(⇐これで動作するかどうかを判断する)
with foo as (
select
s.SalaryID
,s.PayDate
,s.Amount + (2007 - emp.HireFiscalYear) * 1000 as Amount
,s.EmployeeID
from Salary as s
inner join Employees as emp
on s.EmployeeID = emp.EmployeeID
where
s.PayDate = '2008-02-14'
)
insert into Salary
select * from foo where true
on conflict ( SalaryID ) do update set
Amount = excluded.Amount
ポイント
with foo as (
-- ここにテーブル
)
insert into bar
-- where true がポイント
select * from foo where true
on confilict ( key ) do update set
-- excluded がテンポラリーテーブル
baz = excluded.baz
参考
ここの文章は英語が読めなくても雰囲気でいいので端から端まで読むべし!
thanks
rfさん ( twitter id @rf0444 )