0

Pretty new to sqlite (and sql). Trying to modify one table using another.

create table Person(Key1 INTEGER, Key2 INTEGER, Name, IsDead, PRIMARY KEY (Key1,Key2));
create table Zombie(Key1 INTEGER, Key2 INTEGER, PRIMARY KEY (Key1,Key2));

I'd like to update IsDead column based on a list provided in Zombie. SQLite apparently can't use join with an update. Considering UPDATE or REPLACE statements. Thank you in advance.

As per @Tyler Ferraro, below is the solution.

UPDATE Person
SET IsDead = 1
WHERE EXISTS
(
SELECT *
FROM Zombie
WHERE Person.Key1 = Zombie.Key1 and Person.Key2 = Zombie.Key2
);

Below works for a single key, but I don't know how to handle composite keys.

UPDATE Person
SET IsDead = 1
WHERE Key1
IN
(
SELECT Key1
FROM Zombie
);
jobobo
  • 389
  • 5
  • 17
  • Can you give an example of your update statement? What's not working. – Tyler Ferraro Nov 06 '11 at 04:11
  • Possible duplicate of [Update table values from another table with the same user name](https://stackoverflow.com/questions/3845718/update-table-values-from-another-table-with-the-same-user-name) – C-Pound Guru Aug 03 '17 at 18:26

2 Answers2

1

You could take advantage of INSERT OR REPLACE INTO statement.

INSERT OR REPLACE INTO Table1 
    (col1,col2,col3)
select Col1,Col2,Col3
from Table1 t1
inner join Table2 t2 on t1.Col1 = t2.Col1

if you face any duplicate records while using the statement create a unique index on one or more columns based on your logic then the duplication problem won't come up.

CREATE UNIQUE INDEX idxName ON Table1 (Col1,Col2)
Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241
1

I think you'll find the answer you're looking for here: Update table values from another table with the same user name

If you've got the proper Primary Keys setup, which you do, then this should be fairly straight forward.

Community
  • 1
  • 1
Tyler Ferraro
  • 3,753
  • 1
  • 21
  • 28