开发者

How to UPDATE in SQLite using a LEFT JOIN to select candidate rows

开发者 https://www.devze.com 2023-02-03 04:07 出处:网络
I have a table assoc containing columns local_id,开发者_高级运维 remote_id, cachedData I can successfully run an SQLITE query that looks like

I have a table assoc containing columns

local_id,开发者_高级运维 remote_id, cachedData

I can successfully run an SQLITE query that looks like

SELECT a1.local_id, a1.remote_id FROM assoc a1 LEFT JOIN ....

so that I identify certain rows of the assoc table that meet my criteria.

What I would like to do is to set cachedData to null in those rows.

How can I do this? Sqlite doesn't support UPDATE with joins; you can issue subqueries but I can't figure out how to get the syntax correct; it seems nonintuitive to me.


 UPDATE assoc SET cachedData = NULL
    WHERE EXISTS (SELECT * FROM otherTable 
        WHERE otherTable.Col1 = assoc.Col1 AND otherTable.Col2 = assoc.Col1)

Be aware that this is not especially performant.


If assoc has a single column as the primary key (and assuming that it is local_id):

UPDATE assoc
SET cachedData=NULL
WHERE local_id IN (
  SELECT local_id FROM assoc a1 LEFT JOIN ...
);


Aha, there's already a builtin rowid!

UPDATE assoc
SET cachedData=NULL
WHERE rowid IN (
   SELECT rowid FROM assoc a1 LEFT JOIN ...
);
0

精彩评论

暂无评论...
验证码 换一张
取 消