I'm using SQLite and are trying to move old rows from a table Students to Students_Old, and copy new rows from Students_Import.
The problem is I have multiple primary keys as this:
CREATE TABLE "Students" (
`LastName` TEXT NOT NULL,
`FirstName` TEXT NOT NULL,
`BornDate` TEXT NOT NULL,
`Class` TEXT NOT NULL,
`Photo` TEXT,
`ValidUntil` CHAR(10),
PRIMARY KEY(LastName,FirstName,BornDate))
All tables have this structure (except Students_Import that's missing Photo and ValidUntil).
So far I have managed to copy the old rows with this:
INSERT INTO Students_Old
SELECT DISTINCT a.LastName, a.FirstName, a.BornDate, a.Class, a.Photo, a.ValidUntil FROM
Students a LEFT JOIN Students_Import b ON a.LastName =b.LastName AND a.FirstName=b.FirstName AND a.BornDate=b.BornDate WHERE b.LastName is NULL;
And add the new rows with this:
INSERT INTO Students
SELECT DISTINCT a.LastName, a.FirstName, a.BornDate, a.Class, "", "" FROM
Students_Import a LEFT JOIN Students b ON a.LastName =b.LastName AND a.FirstName=b.FirstName AND a.BornDate=b.BornDate WHERE b.LastName is NULL
But I can't figure out how to delete old rows in Students (that don't exist in Students_Import).
I have tried a few variants of this:
DELETE FROM Students WHERE (LastName, FirstName, BornDate) IN
(SELECT DISTINCT a.LastName, a.FirstName, a.BornDate, a.Class, a.Photo, a.ValidUntil FROM
Students a LEFT JOIN Students_Import b ON a.LastName =b.LastName AND a.FirstName=b.FirstName AND a.BornDate=b.BornDate WHERE b.LastName is NULL);
But I only get syntax error or that I can't use it on multiple rows.
I would appreciate any help!