0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SQLiteでUpdate( 他のテーブルの値を使って更新する)

Last updated at Posted at 2019-11-11

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

参考

ここの文章は英語が読めなくても雰囲気でいいので端から端まで読むべし!

SQL As Understood By SQLite

thanks

rfさん ( twitter id @rf0444 )

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?