2012-10-11 81 views
1

我有兩個表更新一列與where子句

線程表

_id recipient_id type 
1  1 
2  1 
3  2 

和地址表

_id address 
1  HELLO 
2  BYE 

recipient_id被映射到地址表的_id連接兩個表後

我需要更新type到一個特定的值,如果地址我你好。我該怎麼做?我已經試過

UPDATE threads SET threads.type = 106 FROM threads 
    INNER JOIN 
    addresses ON addresses._id = threads.recipient_ids 
    AND 
    addresses.address LIKE '%HELLO%'; 

但是我收到錯誤near ".": syntax error。 什麼是更新列的正確語法?

+0

是否支持SQLite'UPDATE FROM'? https://www.sqlite.org/lang_update.html – 2012-10-11 07:01:14

+0

感謝您的信息。我不知道。所以我怎麼能沒有更新從 – nandeesh

+0

更新...在哪裏存在(...) – Arvo

回答

2

可以使用IN操作:

sqlite> .dump 
PRAGMA foreign_keys=OFF; 
BEGIN TRANSACTION; 
CREATE TABLE threads (_id integer primary key, recipient_id integer, type text); 
INSERT INTO "threads" VALUES(1,1,NULL); 
INSERT INTO "threads" VALUES(2,1,NULL); 
INSERT INTO "threads" VALUES(3,2,NULL); 
CREATE TABLE addresses (_id integer primary key, address text); 
INSERT INTO "addresses" VALUES(1,'HELLO'); 
INSERT INTO "addresses" VALUES(2,'BYE'); 
COMMIT; 
sqlite> update threads set type = 106 where _id in 
    ...> (select t._id from threads t, addresses a 
    ...> where t.recipient_id = a._id and a.address like '%HELLO%'); 
sqlite> .dump 
PRAGMA foreign_keys=OFF; 
BEGIN TRANSACTION; 
CREATE TABLE threads (_id integer primary key, recipient_id integer, type text); 
INSERT INTO "threads" VALUES(1,1,'106'); 
INSERT INTO "threads" VALUES(2,1,'106'); 
INSERT INTO "threads" VALUES(3,2,NULL); 
CREATE TABLE addresses (_id integer primary key, address text); 
INSERT INTO "addresses" VALUES(1,'HELLO'); 
INSERT INTO "addresses" VALUES(2,'BYE'); 
COMMIT; 
+0

謝謝。那工作:) – nandeesh