更正SQLite语法 - 使用WHERE EXISTS更新UPDATE

问题描述 投票:10回答:4

我正在尝试更新SQLite表中的列中的选定值。我只想更新符合条件的维护中的单元格,并且必须将单元格更新为从子表中获取的单个值。

我尝试了以下语法,但我只获得了一次单元格更新。我还尝试了替代方案,其中所有单元格都更新为子表格的第一个选定值。

UPDATE maintable
SET value=(SELECT subtable.value FROM maintable, subtable
WHERE  maintable.key1=subtable.key1 AND maintable.key2=subtable.key2)
WHERE EXISTS (SELECT subtable.value FROM maintable, subtable
WHERE  maintable.key1=subtable.key1 AND maintable.key2=subtable.key2)

什么是合适的语法?

sqlite sql-update where exists
4个回答
18
投票

您可以使用update select执行此操作,但一次只能执行一个字段。如果Sqlite支持在update语句上加入会很好,但事实并非如此。

这是一个相关的SO问题,How do I UPDATE from a SELECT in SQL Server?,但对于SQL Server。那里有类似的答案。

sqlite> create table t1 (id int, value1 int);
sqlite> insert into t1 values (1,0),(2,0);
sqlite> select * from t1;
1|0
2|0
sqlite> create table t2 (id int, value2 int);
sqlite> insert into t2 values (1,101),(2,102);
sqlite> update t1 set value1 = (select value2 from t2 where t2.id = t1.id) where t1.value1 = 0;
sqlite> select * from t1;
1|101
2|102

5
投票

您需要使用INSERT OR REPLACE语句,如下所示:

假设维护有4列:key,col2,col3,col4 并且您希望使用子表中的匹配值更新col3

INSERT OR REPLACE INTO maintable
SELECT maintable.key, maintable.col2, subtable.value, maintable.col4
FROM maintable 
JOIN subtable ON subtable.key = maintable.key

0
投票

我们可以使用来自with-clausecolumn-name-list + select-stmt + https://www.sqlite.org/lang_update.html做这样的事情:

CREATE TABLE aa (
_id INTEGER PRIMARY KEY,
a1 INTEGER,
a2 INTEGER);

INSERT INTO aa  VALUES (1,10,20);
INSERT INTO aa  VALUES (2,-10,-20);

--a bit unpleasant because we have to select manually each column and it's just a lot to write
WITH bb (_id,b1, b2)  
AS  (SELECT _id,a1+2, a2+1 FROM aa) 
UPDATE aa  SET a1=(SELECT b1 FROM bb WHERE bb._id=aa._id),a2=(SELECT b2 FROM bb WHERE bb._id=aa._id)
WHERE a1<=1000 OR a2<=2000; --totally useless where clause but just so you don't update every row in aa by putting this where clause in the bb 

--soo now it should be (1,10,20)->(1,12,21) and (2,-10,-20)->(2,-8,-19), and it is
SELECT * FROM aa;


--even better with one select for each row!
WITH bb (_id,b1, b2)  
AS  (SELECT _id,a1+2, a2+1 from aa) 
UPDATE aa  SET (_id,a1,a2)=(SELECT bb._id,b1,b2 FROM bb WHERE bb._id=aa._id)
WHERE a1<=1000 OR a2<=2000; --totally useless where clause but just so you don't update every row in aa by putting this where clause in the bb 

--soo now it should be (1,12,21)->(1,14,22) and (2,-8,-19)->(2,-6,-18), and it is
SELECT * FROM aa;


--you can skip the with altogether
UPDATE aa  SET (_id,a1,a2)=(SELECT bb._id,bb.a1+2, bb.a2+1 FROM aa AS bb WHERE aa._id=bb._id)
WHERE a1<=1000 OR a2<=2000; --totally useless where clause but just so you don't update every row in aa by putting this where clause in the bb 

--soo now it should be (1,14,22)->(1,16,23) and (2,-4,-17)->(2,-6,-18), and it is
SELECT * FROM aa;

希望sqlite足够聪明,不能逐步查询,但根据它的文档。


-1
投票

在这种情况下,它只从维护中的每个原始子表中更新一个子表。错误是当子表包含在SELECT句子中时。

UPDATE可维护的SET值=(SELECT subtable.value FROM table WHERE maintainable.key1 = subtable.key1);

© www.soinside.com 2019 - 2024. All rights reserved.