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

时间:2012-06-06 19:51:37

标签: sqlite sql-update where exists

我正在尝试更新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)

什么是合适的语法?

4 个答案:

答案 0 :(得分:18)

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

这是一个相关的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

答案 1 :(得分: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

答案 2 :(得分:0)

我们可以使用https://www.sqlite.org/lang_update.html中的with-clause + column-name-list + select-stmt做类似的事情:

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很聪明,不会增量查询,但根据文档的要求。

答案 3 :(得分:-1)

在这种情况下,对于主表中的每个原始数据,它仅更新子表中的一个值。 错误是当子表包含在SELECT语句中时。

UPDATE主表 设置值=(选择子表值              从子表              在哪里maintable.key1 = subtable.key1);